Method and system for aggregating objects

ABSTRACT

A method and system for aggregating objects within a computer system are provided. In a preferred embodiment, the method aggregates an enclosed object within an enclosing object. The enclosed object has an object management interface and an external interface, while the enclosing object has a controlling object management interface. The controlling object management interface and the external interface of the enclosed object have query function members for receiving an identifier of an interface and for returning a reference to the identified interface. A preferred embodiment creates an instance of an enclosing object and an object to be enclosed. In static aggregation, the controlling object management interface of the enclosing object knows in advance how to return an identifier to the external interface of the enclosed object. In dynamic aggregation, an object to be enclosed is added to the enclosing object after the enclosing object is instantiated. Once aggregated, when the query function member of the object management interface of the enclosed object receives an identifier of an interface, it invokes the query function member of the controlling object management interface forwarding the interface identifier and returns the reference to an interface returned by the invoked query function member of the controlling object management interface. In dynamic aggregation, rules for determining to which interface to return a reference can be added to the enclosing object and used by the query function member of the controlling object management interface.

CROSS-REFERENCE TO RELATED APPLICATION

This application is a continuation of U.S. patent application Ser. No.08/185,465, filed Jan. 24, 1994, now U.S. Pat. No. 5,805,885 which is acontinuation in part for U.S. patent application Ser. No. 07/996,552,entitled “A Method and System for Aggregating Objects,” which was filedon Dec. 24, 1992, now abandoned and which is hereby incorporated byreference.

TECHNICAL FIELD

This invention relates generally to a computer method and system ofimplementing interfaces to objects and, more specifically, to a methodand system for aggregating objects.

BACKGROUND OF THE INVENTION

As computer software systems increase in sophistication, the cost ofdeveloping the software increases. To minimize the cost of softwaredevelopment, developers often share code. Prior development efforts usethree types of code sharing:

(1) source code sharing, (2) compiled code sharing, and (3) code sharingthrough inheritance.

Also, as computer software systems increase in sophistication, softwareusers are faced with increasing complexity in the maintenance andextensibility of their computer systems. Each time a software vendorgenerates new or improved capabilities, a user who wishes to benefitmust somehow incorporate these modifications. Prior systems generallyrequire such a user to upgrade the software, forcing the user toreinstall at least part of the system. Or, prior systems require asoftware developer to plan in advance for future enhancements andinstall the necessary hooks in the original system to enable the loadingof enhancements at some future time.

Moreover, if a user wishes to enhance the capabilities of a piece ofcurrently owned software by adding capabilities produced by a differentsoftware vendor, the user is limited by what the software vendorsplanned in advance. Prior systems generally require that the two piecesof software be designed to work together and that at least one of thesoftware pieces have knowledge of what capabilities the other provides.Thus, in prior systems, later modifications to code as well as thesharing of code must be accounted for in the software design.

Source and compiled code sharing have been widely used for many years.Source code sharing refers to the use of the same source code by variouscomputer programs or by various versions of the same computer program.For example, a spreadsheet program typically includes source code tocontrol the displaying of a spreadsheet. If a word processing programallows the embedding of a spreadsheet within a document, then the wordprocessing program may use the same (or slightly modified) source codeto display the embedded spreadsheet object. Source code sharing istypically used by a single developer who develops multiple computerprograms. For competitive reasons, developers typically do not sharetheir source code with other developers. Moreover, even if the developerdoes share source code, the recipient of source code typically modifiesthe source code and thus two versions of the source code are maintained.

Compiled code sharing refers to the use of the same compiled code byvarious computer programs. The compiled code is typically stored in astatic or dynamic link library. Compiled code stored in a static linklibrary is shared when a computer program is linked before execution.Compiled code stored In a dynamic link library is shared when a computerprogram is linked during execution. The developer of a spell checkingprogram, for example, may share compiled code by compiling the programand storing the compiled code in a static link library. The static linklibrary can then be distributed to developers of word processingprograms who can link the compiled spell checking code into their wordprocessing program. The developer of the spell checking programtypically needs to modify the compiled code to meet special requirementsof certain developers. These modifications tend to increase thecomplexity (and size) of the compiled code and may conflict withrequirements of other recipients. Alternatively, the developer coulddistribute multiple versions of the static link library. However, themaintenance of multiple versions can be costly.

Object-oriented programming techniques employ a concept referred to asinheritance to allow the sharing of code. An overview of well-knownobject-oriented programming techniques is provided, since the presentinvention is described below using object-oriented programming. Twocommon characteristics of object-oriented programming languages aresupport for data encapsulation and data type inheritance. Dataencapsulation refers to the binding of functions and data. Inheritancerefers to the ability to declare a data type in terms of other datatypes.

In the C++ language, object-oriented techniques are supported throughthe use of classes. A class is a user-defined type. A class declarationdescribes the data members and function members of the class. Forexample, the following declaration defines data members and a functionmember of a class named CIRCLE.

class CIRCLE { public:  int x, y;  int radius;  void draw(); };

Variables x and y specify the center location of a circle and variableradius specifies the radius of the circle. These variables are referredto as data members of the class CIRCLE. The function draw is auser-defined function that draws the circle of the specified radius atthe specified location. The function draw is referred to as a functionmember of class CIRCLE. The data members and function members of a classare bound together in that the function operates on an instance of theclass. An instance of a class is also called an object of the class.

In the syntax of C++, the following statement declares the objects a andb to be of type class CIRCLE.

CIRCLE a, b;

This declaration causes the allocation of memory for the objects a andb. The following statements assign data to the data members of objects aand b.

a.x=2;

a.y=2;

a.radius=1;

b.x=4;

b.y=5;

b.radius=2;

The following statements are used to draw the circles defined by objectsa and b.

a.draw( );

b.draw( );

A derived class is a class that inherits the characteristics—datamembers and function members—of its base classes. For example, thefollowing derived class CIRCLE_FILL inherits the characteristics of thebase class CIRCLE.

class CIRCLE_FILL : CIRCLE { public:  int pattern;  void fill(); };

This declaration specifies that class CIRCLE_FILL includes all the dataand function members that are in class CIRCLE in addition to those dataand function members introduced in the declaration of class CIRCLE_FILL,that is, data member pattern and function member fill. In this example,class CIRCLE_FILL has data members x, y, radius, and pattern andfunction members draw and fill. Class CIRCLE_FILL is said to “inherit”the characteristics of class CIRCLE. A class that inherits thecharacteristics of another class is a derived class (e.g., CIRCLE_FILL).A class that does not inherit the characteristics of another class is aprimary (root) class (e.g., CIRCLE). A class whose characteristics areinherited by another class is a base class (e.g., CIRCLE is a base classof CIRCLE_FILL). A derived class may inherit the characteristics ofseveral classes, that is, a derived class may have several base classes.This is referred to as multiple inheritance.

A derived class may specify that a base class is to be inheritedvirtually. Virtual inheritance of a base class means that only oneinstance of the virtual base class exists in the derived class. Forexample, the following is an example of a derived class with twononvirtual base classes.

class CIRCLE_1: CIRCLE { . . . };

class CIRCLE_2: CIRCLE { . . . };

class PATTERN: CIRCLE_1, CIRCLE_2 { . . . };

In this declaration class PATTERN inherits class CIRCLE twicenonvirtually through classes CIRCLE_1 and CIRCLE_2. There are twoinstances of class CIRCLE in class PATTERN.

The following is an example of a derived class with two virtual baseclasses.

class CIRCLE_1: virtual CIRCLE { . . . };

class CIRCLE_2: virtual CIRCLE { . . . };

class PATTERN: CIRCLE_1, CIRCLE_2 { . . . };

The derived class PATTERN inherits class CIRCLE twice virtually throughclasses CIRCLE_1 and CIRCLE_2. Since the class CIRCLE is virtuallyinherited twice, there is only one object of class CIRCLE in the derivedclass PATTERN. One skilled in the art would appreciate virtualinheritance can be very useful when the class derivation is morecomplex.

A class may also specify whether its function members are virtual.Declaring that a function member is virtual means that the function canbe overridden by a function of the same name and type in a derivedclass. In the following example, the function draw is declared to bevirtual in classes CIRCLE and CIRCLE_FILL.

class CIRCLE { public:  int x, y;  int radius;  virtual void draw(); };class CIRCLE_FILL : CIRCLE { public:  int pattern;  virtual void draw();};

If a virtual function is declared without providing an implementation,then it is referred to as a pure virtual function. A pure virtualfunction is a virtual function declared with the pure specifier, “=0”.If a class specifies a pure virtual function, then any derived classneeds to specify an implementation for that function member before thatfunction member may be invoked.

In order to access objects, the C++ language provides a pointer datatype. A pointer holds values that are addresses of objects in memory.Through a pointer, an object can be referenced. The following statementdeclares variable c_ptr to be a pointer on an object of type classCIRCLE and sees variable c_ptr to hold the address of object c.

CIRCLE *c_ptr;

c_ptr=&c;

Continuing with the example, the following statement declares object ato be of type lass CIRCLE and object b to be of type class CIRCLE_FILL.

CIRCLE a;

CIRCLE_FILL b;

The following statement refers to the function draw as defined in classCIRCLE.

a.draw( );

Whereas, the following statement refers to the function draw defined inclass CIRCLE_FILL.

b.draw( );

Moreover, the following statements type cast object b to an object oftype class CIRCLE and invoke the function draw that is defined in classCIRCLE_FILL.

CIRCLE *c_ptr;

c_ptr=&b;

c_ptr->draw( ); // CIRCLE_FILL::draw( )

Thus, the virtual function that is called is function CIRCLE_FILL::draw.

FIG. 1 is a block diagram illustrating typical data structures used torepresent an object. An object is composed of instance data (datamembers) and member functions, which implement the behavior of theobject. The data structures used to represent an object compriseinstance data structure 101, virtual function table 102, and thefunction members 103, 104, 105. The instance data structure 101 containsa pointer to the virtual function table 102 and contains data members.The virtual function table 102 contains an entry for each virtualfunction member defined for the object. Each entry contains a referenceto the code that implements the corresponding function member. Thelayout of this sample object conforms to the model defined in U.S.patent application Ser. No. 07/682,537, entitled “A Method forImplementing Virtual Functions and Virtual Bases in a Compiler for anObject Oriented Programming Language,” which is hereby incorporated byreference. In the following, an object will be described as an instanceof a class as defined by the C++ programming language. One skilled inthe art would appreciate that objects can be defined using otherprogramming languages.

The inheritance of a class is a type of code sharing. A developer of aclass can provide the implementation of the class to other developers.These other developers can then create classes that derive from theclass provided. Thus, the function members of the provided class areshared. If, however, a class is inherited and a virtual function isoverridden, then the testing of the overriding virtual function can becomplex. The overriding virtual function can modify the state of theobject in a way that affects non-overridden functions. Thus, eachinherited function must be independently tested in conjunction with thetesting of the overriding virtual function. To ameliorate thecomplexities of testing, the developers of a class implementation maydistribute source code with the implementation. Unfortunately, thedistribution of source code has the same drawbacks to sharing sourcecode as discussed above.

An advantage of using object-oriented techniques is that thesetechniques can be used to facilitate the sharing of objects. Inparticular, object-oriented techniques facilitate the creation ofcompound documents. A compound document is a document that containsobjects generated by various computer programs. (Typically, only thedata members of the object and the class type are stored in a compounddocument.) For example, a word processing document that contains aspreadsheet object generated by a spreadsheet program is a compounddocument. A word processing program allows a user to embed a spreadsheetobject (e.g., a cell) within a word processing document. To allow thisembedding, the word processing program is compiled using the classdefinition of the object to be embedded to access function members ofthe embedded object. Thus, the word processing program would need to becompiled using the class definition of each class of objects that can beembedded in a word processing document. To embed an object of a newclass into a word processing document, the word processing program wouldneed to be recompiled with the new class definition. Thus, only objectsof classes selected by the developer of the word processing program canbe embedded. Furthermore, new classes can only be supported with a newrelease of the word processing program.

To allow objects of an arbitrary class to be embedded into compounddocuments, interfaces are defined through which an object can beaccessed without the need for the word processing program to have accessto the class definitions at compile time. An abstract class is a classin which there is at least one virtual function member with noimplementation (a pure virtual function member). An interface is anabstract class with no data members and whose virtual functions are allpure. Thus, an interface provides a protocol for two programs tocommunicate. Interfaces are typically used for derivation: a programimplements classes that provide implementations for the interfaces theclasses are derived from. Thereafter, objects are created as instancesof these derived classes.

The following class definition is an example definition of an interface.In this example, for simplicity of explanation, rather than allowing anyclass of object to be embedded in its documents, a word processingprogram allows spreadsheet objects to be embedded. Any spreadsheetobject that provides this interface can be embedded, regardless of howthe object is implemented. Moreover, any spreadsheet object, whetherimplemented before or after the word processing program is compiled, canbe embedded.

class ISpreadSheet { virtual void File() = 0; virtual void Edit() = 0;virtual void Formula() = 0; virtual void Format() = 0; virtual voidGetCell (string RC, cell *pCell) = 0; virtual void Data() = 0; }

The developer of a spreadsheet program would need to provide animplementation of the interface to allow the spreadsheet objects to beembedded in a word processing document.

When the word processing program embeds a spreadsheet object, theprogram needs access to the code that implements the interface for thespreadsheet object. To access the class code, each implementation isgiven a unique class identifier. For example, code implementing aspreadsheet object developed by Microsoft Corporation may have a classidentifier of “MSSpreadsheet,” while code implementing a spreadsheetobject developed by another corporation may have a class identifier of“LTSSpreadsheet.” A persistent registry in each computer system ismaintained that maps each class identifier to the code that implementsthe class. Typically, when a spreadsheet program is installed on acomputer system, the persistent registry is updated to reflect theavailability of that class of spreadsheet objects. So long as aspreadsheet developer implements each function member defined by theinterface and the persistent registry is maintained, the word processingprogram can embed instances of the developer's spreadsheet objects intoa word processing document. The word processing program accesses thefunction members of the embedded spreadsheet objects without regard towho has implemented them or how they have been implemented.

Various spreadsheet developers may wish, however, to implement onlycertain function members. For example, a spreadsheet developer may notwant to implement database support, but may want to support all otherfunction members. To allow a spreadsheet developer to support only someof the function members, while still allowing the objects to beembedded, multiple interfaces for spreadsheet objects are defined. Forexample, the interfaces IDatabase and IBasic may be defined for aspreadsheet object as follows.

class IBasic virtual void File() = 0; virtual void Edit() = 0; virtualvoid Formula() = 0; virtual void Format() = 0; virtual voidGetCell(string RC, cell *pCell) = 0; } class IDatabase { virtual voidData() = 0; }

Each spreadsheet developer would implement the IBasic interface and,optionally, the Database interface.

At run time, the word processing program would need to determine whethera spreadsheet object to be embedded supports the IDatabase interface. Tomake this determination, another interface is defined (that everyspreadsheet object implements) with a function member that indicateswhich interfaces are implemented for the object. This interface is namedIUnknown (and referred to as the unknown interface or the objectmanagement interface) and is defined as follows.

class IUnknown { virtual HRESULT QueryInterface (REFIID iid, void **ppv)= 0;   virtual ULONG AddRef( ) = 0;   virtual ULONG Release ( ) = 0; }

The IUnknown interface defines the function member (method)QueryInterface. The method QueryInterface is passed an interfaceidentifier (e.g., “IDatabase”) in parameter iid (of type REFIID) andreturns a pointer to the implementation of the identified interface forthe object for which the method is invoked in parameter ppv. If theobject does not support the interface, then the method returns a false.The type HRESULT indicates a predefined status, the type REFIIDindicates a reference to an interface identifier, and the type ULONGindicates an unsigned long integer.

CODE TABLE 1 HRESULT XX::QueryInterface(REFIID iid, void **ppv) { ret =TRUE; switch (iid) { case IID_IBasic: *ppv = piBasic; break; caseIID_IDatabase: *ppv = piDatabase; break; case IID_IUnknown: *ppv = this;break; default ret = FALSE; } if (ret == TRUE) {AddRef( );}; return ret;}

Code Table 1 contains C++ pseudocode for a typical implementation of themethod QueryInterface for class XX, which inherits the class IUnknown.If the spreadsheet object supports the IDatabase interface, then themethod QueryInterface includes the appropriate case label within theswitch statement. The variables pIBasic and pIDatabase point to apointer to the virtual function tables of the IBasic and IDatabaseinterfaces, respectively. The method QueryInterface invokes to methodAddRef (described below) to increment a reference count for the objectof class XX when a pointer to an interface is returned.

CODE TABLE 2 void XX::AddRef() {refcount++;} void XX::Release(){if(--refcount==0) delete this;}

The interface IUnknown also defines the methods AddRef and Release,which are used to implement reference counting. Whenever a new referenceto an interface is created, the method AddRef is invoked to increment areference count of the object. Whenever a reference is no longer needed,the method Release is invoked to decrement the reference count of theobject and, when the reference count goes to zero, to deallocate theobject. Code Table 2 contains C++ pseudocode for a typicalimplementation of the methods AddRef and Release for class XX, whichinherits the class IUnknown.

The IDatabase interface and IBasic interface inherit the IUnknowninterface. The following definitions illustrate the use of the IUnknowninterface.

class IDatabase : public IUknown { public: virtual void Data( ) = 0; }class IBasic : public IUnknown { public: virtual void File( ) = 0;virtual void Edit( ) = 0; virtual void Formula( ) = 0; virtual voidFormat( ) = 0; virtual void GetCell (string RC, cell *pCell) = 0; }

FIG. 2 is a block diagram illustrating a sample data structure of aspreadsheet object using nested classes. The spreadsheet objectcomprises object data structure 201, IBasic interface data structure203, IDatabase interface data structure 204, the virtual function tables202, 205, 206 and methods 207 through 221. The object data structure 201contains a pointer to the virtual function table 202 and pointers to theIBasic and IDatabase interface. Each entry in the virtual function table202 contains a pointer to a method of the IUnknown interface. The IBasicinterface data structure 203 contains a pointer to the virtual functiontable 205. Each entry in the virtual function table 205 contains apointer to a method of the IBasic interface. The IDatabase interfacedata structure 204 contains a pointer to the virtual function table 206.Each entry in the virtual function table 207 contains a pointer to amethod of the IDatabase interface. Since the IBasic and IDatabaseinterfaces inherit the IUnknown interface, each virtual function table205 and 206 contains a pointer to the methods QueryInterface, AddRef,and Release. In the following, an object data structure is representedby the shape 222 labeled with the interfaces through which the objectmay be accessed.

The following pseudocode illustrates how a word processing programdetermines whether a spreadsheet object supports the IDatabaseinterface.

if (pSpreadsheet->QueryInterface(“IDatabase”, &pIDatabase))

// IDatabase supported

else

// IDatabase not supported

The pointer pSpreadsheet is a pointer to an instance of the spreadsheetclass shown in FIG. 2. (pSpreadsheet points to data structure 201.) Ifthe object supports the IDatabase interface, the method QueryInterfacedefined by method 207 sets the pointer pIDatabase to point to theIDatabase data structure 204 and returns true as its value.

Normally, an object can be instantiated (an instance of the objectcreated in memory) by a variable declaration or by the “new” operator.However, both techniques of instantiation need the class definition atcompile time. A different technique is needed to allow a word processingprogram to instantiate a spreadsheet object at run time. One techniqueprovides a global function CreateInstanceXX, which is defined in thefollowing.

static void CreateInstanceXX (REFIID iid, void **ppv)=0;

The method CreateInstanceXX instantiates an object of class XX andreturns a pointer ppv to the interface of the object designated byparameter iid.

SUMMARY OF THE INVENTION

It is a goal of the present invention to provide a method and system foraggregating objects.

It is another goal of the present invention to provide a method andsystem for dynamically modifying object behavior.

It is another goal of the present invention to provide a method andsystem for dynamically aggregating objects.

It is another goal of the present invention to provide a method andsystem for statically aggregating objects.

It is another goal of the present invention to provide a method andsystem for enclosing an object within another object while exposing aninterface of the enclosed object to a client of the enclosing object.

It is another goal of the present invention to provide a method andsystem for enclosing an object within another object after the enclosingobject is instantiated.

It is another goal of the present invention to provide a method andsystem for dynamically combining objects of different types into asingle object.

It is another goal of the present invention to provide a method andsystem for implementing an object that can be either enclosed withinanother object or not enclosed within another object without modifyingthe implementation of the object.

It is another goal of the present invention to provide a method andsystem for implementing an aggregate object so that a client is unawarethat the object is an aggregate.

It is another goal of the present invention to provide a method andsystem for enclosing objects wherein an enclosed object can itself be anenclosing object to an arbitrary level of enclosing.

It is another goal of the present invention to provide a method andsystem for enhancing a base object's behavior by adding a new interfaceto it.

It is another goal of the present invention to provide a method andsystem for enhancing a base object's apparent behavior by adding aninterface to it that overrides standard behavior of the base object.

It is another goal of the present invention to provide a method andsystem for supplying default functionality to objects by enclosing themwithin an enclosing object where an enclosed or enclosing objectimplements the default functionality.

It is another goal of the present invention to provide a method andsystem for implementing controlling behavior over common functionalitypresent in enclosed objects.

It is another goal of the present invention to provide a method andsystem for determining which interface to provide to a client when theclient requests an interface that is implemented by more than oneenclosed object.

These and other goals, which will be apparent as the invention is morefully described below, are provided by a method and system foraggregating objects within a computer system. In a preferred embodiment,the method aggregates an enclosed object within an enclosing object. Theenclosed object has an object management interface and one or moreexternal interfaces, while the enclosing object has a controlling objectmanagement interface. Each interface exposed to a client by theaggregate object has a query function member for receiving an identifierof an interface and for returning a reference to the identifiedinterface. The query function member of the controlling objectmanagement interface of the enclosing object receives an identifier ofan interface exposed by the enclosing object and returns a reference tothe exposed interface. A preferred method creates an instance of theenclosed object. When, the query function member of an exposed interfaceof the enclosed object receives an identifier of an interface, itinvokes the query function member of the controlling object managementinterface of the enclosing object passing the received identifier andreturns the reference returned by the invoked query function member ofthe controlling object management interface of the enclosing object as areference to the identified interface.

In a preferred embodiment of static aggregation, a query function memberof an enclosed object is implemented with knowledge of the externalinterfaces of the enclosed object and has no knowledge of interfaces(other than the controlling object management interface) of theenclosing object or other enclosed objects. The query function member ofa controlling object management interface of the enclosing object isimplemented with knowledge of the exposed interfaces of enclosedobjects.

In a preferred embodiment of dynamic aggregation, an object can bemodified dynamically by allowing interface instances, as implemented byobjects, to be aggregated during the execution of a client program.Interfaces are aggregated by dynamically enclosing the objects thatimplement them into a multitype object. Each interface to be added isimplemented by an object which has the ability to be aggregated. Amultitype object is created to act as an enclosing object. The multitypeobject has an add interface function member, which can be used toaggregate interfaces by adding them to the enclosing multitype object.The multitype object also has an add object function member foraggregating all of the interfaces of an object. The multitype objectalso has a query function member for retrieving references to the addedinterfaces upon request from a client. This query function member ispart of the controlling object management interface of the enclosingmultitype object. Also, an instance of an object that implements theinterface to be aggregated is created. During creation, a pointer to theenclosing multitype object is passed to the object to be enclosed toenable the enclosed object to communicate with the enclosing multitypeobject. The created object implementing the interface to be aggregatedhas a query function member, which supports retrieval of a reference tothe interface to be aggregated. A preferred method invokes the addinterface function member or the add object function member of theenclosing multitype object passing it a reference to the created objectimplementing the interface to be aggregated. Later, the query functionmember of the enclosing multitype object is invoked in order to retrievea reference to the interface that has been aggregated.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a block diagram illustrating typical data structures used torepresent an object.

FIG. 2 is a block diagram illustrating a sample data structure of aspreadsheet object using nested classes.

FIG. 3 is a block diagram showing an aggregate object.

FIG. 4 is a block diagram of the data structure layout of an instance ofan object of class S1.

FIG. 5 is a block diagram of the data structure layout of an object ofclass S3.

FIGS. 6A and 6B are block diagrams illustrating the cooperation betweenan enclosing object and an enclosed object.

FIGS. 7A, 7B, and 7C are block diagrams of the sequence of adding twoobjects to a multitype object

FIG. 8 is a block diagram of the data structure layout of an instance ofan object of class S1.

FIG. 9 is a flow diagram of the method AddInterface of the IMultitypeinterface implemented by a multitype object.

FIG. 10 is a flow diagram of the method QueryInterface of thecontrolling IUnknown interface for a multitype object.

FIG. 11 is a block diagram showing the data structure layout of amultitype object, corresponding to FIG. 7C, after the IBasic, IPrint,and IDatabase interfaces have been dynamically aggregated using themethod AddObject.

FIG. 12 is a pictorial representation of a spreadsheet object and adatabase query object, which can be aggregated together to create anattached database query object.

FIG. 13 is a block diagram of an aggregated attached database queryobject.

DETAILED DESCRIPTION OF THE INVENTION

The present invention provides a method in a computer system foraggregating objects. Objects can either be aggregated statically ordynamically. Using static aggregation, an enclosing object typically hascompile time knowledge of the exposed interfaces of enclosed objects.The object management interface of the enclosing object is thereforecustomized to return interface pointers to exposed interfaces ofenclosed objects using this knowledge. Instances of these staticallyaggregated objects are created dynamically (at run time).

Using dynamic aggregation, an enclosing object is instantiated and canbe used to aggregate objects or interfaces at run time. The enclosingobject has no a priori knowledge of the enclosed objects or interfaces,thus no compile time knowledge is used by the enclosing object.Similarly, the enclosed objects and interfaces have no knowledge of theimplementation or the presence of interfaces of the enclosing object,with the exception of the controlling object management interface usedto aggregate objects and interfaces. Also, a rules mechanism is providedto control access to aggregated objects and interfaces.

Each of these types of aggregation is discussed in turn in the followingsections. In a preferred embodiment, the methods and systems of thepresent invention are implemented on a computer system comprising acentral processing unit, memory, and input/output devices.

Static Aggregation

In a preferred embodiment of static aggregation, an aggregate objectprovides a plurality of interfaces to its clients. The computer programthat instantiates an object is referred to as a client. An aggregateobject comprises one or more enclosed objects and an implementation ofthe IUnknown interface, which is referred to as the controlling IUnknowninterface of the aggregate object. An aggregate object exposes to itsclients its own interfaces and interfaces from the enclosed objects. Themethod QueryInterface of the controlling IUnknown interface returns apointer to each interface exposed by the aggregate object. The aggregateobject instantiates each enclosed object. This instantiation can beperformed during construction of the aggregate object or can bepostponed until an interface of the enclosed object is requested. Eachenclosed object contains a pointer to the controlling IUnknowninterface. The method QueryInterface of an exposed interface of anenclosed object is preferably implemented to invoke the methodQueryInterface of an IUnknown interface. When the enclosed object isimplemented, the developer typically has no knowledge of what interfacesthe enclosing object may expose. Consequently, the method QueryInterfaceof an enclosed object invokes the method QueryInterface of thecontrolling IUnknown interface to retrieve a pointer to the requestedinterface. The method QueryInterface of the controlling IUnknowninterface is typically implemented with knowledge of all the exposedinterfaces. When an object is not enclosed, the controlling IUnknowninterface is the IUnknown interface of the object. Conversely, when anobject is enclosed, the controlling IUnknown interface is the IUnknowninterface of the enclosing object.

In a preferred embodiment, an aggregate object maintains a referencecount. When the aggregate object is instantiated, its reference count isset to one. The method QueryInterface of the controlling IUnknownincrements the reference count when a reference is returned to theclient. The method AddRef of an exposed interface of an enclosed objectinvokes the method AddRef of the controlling IUnknown interface toincrement the reference count of the aggregate object. Similarly, themethod Release of an exposed interface of an enclosed object invokes themethod Release of the controlling IUnknown interface to decrement thereference count of the aggregate object and delete the aggregate objectwhen the reference count equals zero. When an enclosed object isinstantiated, the reference count of the enclosed object is set to one.When the aggregate object is deleted, the method Release of the IUnknowninterface of each enclosed object is invoked to delete the enclosedobject.

FIG. 3 is a block diagram showing an aggregate object. The aggregateobject S3 exposes interfaces A, B, C, F, and the controlling IUnknown.The aggregate (enclosing) object S3 comprises enclosed object S1 303,enclosed object S2 302, and implementation I3 304. The enclosed objectS1 implements external interfaces C and D, and the enclosed object S2implements external interfaces E and F. (An external interface is aninterface of an object that can be exposed by an enclosing object. Aninternal interface is an interface of an object that cannot be exposedby an enclosing object.) The implementation I3 implements externalinterfaces A, B, and the controlling IUnknown. A client of the aggregateobject S3 need not be aware that the object is an aggregate. Theaggregate object S3 instantiates objects S1 and S2 either duringconstruction of aggregate object S3 or at a later time. Theimplementation I3 contains pointers to the IUnknown interfaces ofobjects S1 and S2. Objects S1 and S2 are initialized to contain apointer to the controlling IUnknown interface.

The method QueryInterface of an exposed interface can return a pointerto each exposed interface and increments the reference count of theaggregate object when a pointer is returned. The method QueryInterfaceof the controlling IUnknown has direct access to the pointers to theinterfaces—A, B, and controlling IUnknown—that implementation I3implements and invokes the method QueryInterface of the IUnknowninterface of the enclosed objects to retrieve pointers to the exposedinterfaces—C and F—of enclosed objects S1 and S2. When a pointer to anexposed interface is returned, the method QueryInterface of thecontrolling IUnknown interface increments the reference count of theaggregate object S3 by invoking the method AddRef of the controllingIUnknown interface. The method QueryInterface of each exposed interface(other than the controlling IUnknown interface) preferably invokes themethod QueryInterface of the controlling IUnknown interface.

CODE TABLE 3 void CreateInstanceS1 (IUnknown *punkOuter, REFIID iid,void **ppv) { IUnknown *punk; S1::CreateInstance (punkOuter, &punk);punk—>QueryInterface (iid, ppv); punk—>Release ( ); } class IC: publicIUnknown { // methods of IC} class ID: public IUnknown { // methods ofID} class SI: public IUnknown { public: static voidCreateInstance(IUnknown *punkOuter, IUnknown **ppunk) { S1 *pS1 = newS1(punkOuter); pS1—>QueryInterface(IID_IUnknown, ppunk); } private: voidS1(IUnknown *punkOuter) : m_C(this), m_D(this) { if (punkOuter == NULL)m_punkOuter = this; else m_punkOuter = punkOuter; m_refcount = 0; }class C: public IC { public: void C(S1 *pS1) {m_pS1 = pS1;} virtualboolean QueryInterface (REFIID iid, void **ppv) { returnm_pS1—>m_punkOuter—>QueryInterface(iid, ppv);} virtual void AddRef( ) {m_pS1—>m_punkOuter—>AddRef( );} virtual void Release( ) {m_pS1—>m_punkOuter—>Release( );} // other methods of IC private: S1*m_pS1; } friend C; C m_C; class D: public ID { public void D(S1 *pS1){m_pS1 = pS1;} virtual boolean QueryInterface (REFIID iid, void **ppv) {return m_pS1—>m_punkOuter—>QueryInterface(iid, ppv);} virtual voidAddRef( ) { m_pS1—>m_punkOuter—>AddRef( );} virtual void Release( ) {m_pS1—>m_punkOuter—>Release( );} // other methods of ID private: S1*m_pS1; } friend D; D m_D; public: virtual boolean QueryInterface(REFIID iid, void **ppv) { ret = TRUE; switch (iid) { case IID_C: *ppv =&m_C; m_punkOuter—>AddRef( ); break; case IID_D: *ppv = &m_D;m_punkOuter—>AddRef( ); break; case IID_IUnknown; *ppv = this; AddRef(); break; default; ret = FALSE; } return ret; } virtual void AddRef(){  m_refcount++;} virtual void Release( ) {if(−−m_refcount == 0) deletethis;} private: IUnknown *m_punkOuter; int m_refcount; }

Code Table 3 contains C++ pseudocode for a preferred class definition ofthe object S1, which can be enclosed in an aggregate (an aggregatableobject) along with a global function that creates an instance of theobject. The classes IUnknown, IC, and ID are interfaces that define themethods of each interface. The class S1 implements the IUnknowninterface, the IC interface, and the ID interface. The class S1implements the IC and ID interfaces as external interfaces. FIG. 4 is ablock diagram of the data structure layout of an instance of an objectof class S1. Instance structure 401 contains the data members of classS1 (m_C, m_D, m_punkOuter, m_refcount) and a pointer to the virtualfunction table pointer (S1::vfptr). The data members m_C and m_D areinstances of an object of classes C and D, respectively. Classes C and Dare friends of class S1, which allows C and D objects to access theprivate members of class S1. The virtual function table pointerS1::vfptr points to virtual function table 402, the virtual functiontable pointer within data member m_C S1::C::vfptr points to virtualfunction table 403, and the virtual function table pointer within datamember m_D S1::D::vfptr points to virtual function table 403A. Virtualfunction table 402 contains pointers to the virtual functions definedfor the IUnknown interface, virtual function table 403 contains pointerto the virtual functions defined for the C interface, and virtualfunction table 403A contains pointers to the virtual functions definedfor D interface. The ellipsis in virtual function tables 403 and 403Aindicates pointers to additional function members of classes C and D,respectively. Functions 404 through 408 are the function members ofclass S1. Function 407 is the constructor for class S1. Function 408 isthe function CreateInstance for class S1. Functions 409 through 412 arethe function members of class C. Function 412 is the constructor forclass C. Functions 413 through 416 are the function members of class D.Function 416 is the constructor for class D.

As shown in Code Table 3, the method S1::QueryInterface returns apointer to the interface C, the interface D, or the interface IUnknown.When a pointer to the interface C or interface D is returned, the methodS1::QueryInterface invokes the method S1::AddRef to increment thereference count for the S1 object. The method S1::AddRef increments thereference count, and the method S1::Release decrements the referencecount and deletes the S1 object when the reference count is zero. When apointer to the interface C or interface D is returned, the methodS1::QueryInterface invokes the method AddRef of the controlling IUnknowninterface, which when the S1 object is not aggregated is the methodS1::AddRef.

The global function CreateInstanceS1 creates an instance of an object ofclass S1. A client invokes this function to instantiate an object ofclass S1. Thus, a client can instantiate an object of class S1 withouthaving access to the S1 class definition at compile time or run time.The function CreateInstanceS1 is passed a pointer to the controllingIUnknown (punkOuter) when the instantiated S1 object is enclosed withinan aggregate object and an identifier (iid) of an interface to return.The function CreateInstanceS1 returns a pointer (ppv) to the identifiedinterface. The function CreateInstanceS1 invokes the methodS1::CreateInstance passing the parameter punkOuter. The methodS1::CreateInstance instantiates an S1 object and returns a pointer(punk) to the IUnknown interface of the S1 object. The functionCreateInstanceS1 invokes the method QueryInterface of the S1 object toretrieve a pointer to the identified interface. The functionCreateInstanceS1 then invokes the method Release of the S1 objectbecause the temporary pointer punk is no longer needed.

The method S1::CreateInstance instantiates an S1 object and returns apointer (ppunk) to the IUnknown interface of the S1 object. The methodS1::CreateInstance is passed a pointer (punkOuter) to the controllingIUnknown. The method S1::CreateInstance uses operator new to instantiatethe S1 object. During instantiation, the constructor S1::S1 is invokedand passed the value of the parameter punkOuter. After the S1 object isconstructed, the method S1::CreateInstance invokes the methodS1::QueryInterface to retrieve a pointer to the IUnknown interface ofthe S1 object.

The constructor S1::S1 initializes the data members m_C, m_D,m_punkOuter, and m_refcount. The constructor S1::S1 is passed theparameter punkOuter. During instantiation of the data members m_C andm_D, the constructors C::C and D::D are invoked and passed the thispointer for the S1 object. If the value of the parameter punkOuter isNULL, the constructor S1::S1 sets the data member m_punkOuter to thevalue of the this pointer (which points to the newly instantiated S1object). If the value of the parameter punkOuter is non-NULL, theconstructor S1::S1 sets the data member m_punkOuter to the value ofparameter punkOuter. Data member m_punkOuter points to the value of thecontrolling IUnknown of the aggregate when the S1 object is enclosed andpoints to the controlling IUnknown of the S1 object when the S1 objectis not enclosed. The constructor S1::S1 also initializes the data memberm_refcount to zero.

The constructor C::C is passed a pointer to the S1 object. Theconstructor C::C stores the passed pointer in data member C::m_pS1. Thedata member C::m_pS1 is used by the methods of class C to access thedata member S1::m_punkOuter.

The methods C::QueryInterface, C::AddRef, and C::Release invoke thecorresponding methods of the IUnknown interface pointed to by datamember S1::m_punkOuter, which when the S1 object is enclosed, points tothe controlling IUnknown interface of the aggregate.

The constructor and other methods of class D are analogous to those ofclass C.

FIG. 4 shows an instance of an S1 object that is not part of anaggregate. The data members S1::C::m_pS1, S1::D::m_pS1, andS1::m_punkOuter are initialized to point to the S1 object itself. Themethods QueryInterface, AddRef, and Release of the data members m_C andm_D invoke the IUnknown methods of the interface of the S1 object.

The S2 object that implements interfaces E and F is analogous to the S1object as described above.

CODE TABLE 4 void CreateInstanceS3 (IUnknown *punkOuter, REFIID iid,void **ppv) { IUnknown *punk; S3::CreateInstance (punkOuter, &punk);punk—>Query Interface (iid, ppv); punk—>Release ( ); } class IA: publicIUnknown { // methods of class IA} class IB: public IUnknown { //methods of class IB} class S3: public IUnknown { public: static voidCreateInstance(IUnknown *punkOuter, IUnknown **ppunk) { S3 *pS3 = newS3(punkOuter); CreateInstanceS1(pS3—>m_punkOuter, IID_IUnknown,pS3—>m_punkS1); CreateInstanceS2(pS3—>m_punkOuter, IID_IUnknown,pS3—>m_punkS2); pS3—>QueryInterface(IID_IUnknown, ppunk);} private: voidS3(IUnknown *punkOuter) : m_A(this), m_B(this) { if(punkOuter == NULL)m_punkOuter = this; else m_punkOuter = punkOuter; m_refcount = 0;} void˜S3( ) {m_punkS1—>Release( ); m_punkS2—>Release( );} class A: public IA{ public: void A(S3 *pS3) {m_pS3 = pS3} virtual boolean QueryInterface(REFIID iid, void **ppv) { returnm_pS3—>m_punkOuter—>QueryInterface(iid, ppv);} virtual void AddRef( ) {m_pS3—>m_punkOuter—>AddRef( );} virtual void Release( ) {m_pS3—>m_punkOuter—>Release( );} \\ other methods of IA private: S3*m_pS3; }; friend A; A m_A; class B: public IB { public: void B(S3 *pS3){m_pS3 = pS3} virtual boolean QueryInterface (REFIID iid, void **ppv) {return m_pS3—>m_punkOuter—>QueryInterface(iid, ppv);} virtual voidAddRef( ) { m_pS3—>m_punkOuter—>AddRef( );} virtual void Release( ) {m_pS3—>m_punkOuter—>Release( );} \\ other methods of IB private: S3*m_pS3; }; friend B; B m_B public: virtual boolean QueryInterface(REFIIDiid, void **ppv) { ret = TRUE; switch (iid) { case IID_C: ret =m_punkS1—>QueryInterface(iid, ppv); break; case IID_F: ret =m_punkS2—>QueryInterface(iid, ppv); break; case IID_A: *ppv = &m_A;m_punkOuter—>AddRef( ); break; case IID_B: *ppv = &m_B;m_punkOuter—>AddRef( ); break; case IID_IUnknown: *ppv_this; AddRef( );break; default: ret = FALSE; } return ret; } virtual void AddRef( ) {m_refcount++;} virtual void Release( ) {if (−−m_refcount == 0) deletethis;} private: IUnknown *m_punkOuter; int m_refcount; IUnknown*m_punkS1; IUnknown *m_punkS2; };

Code Table 4 contains C++ pseudocode for a preferred class definition ofan aggregate object. The class S3 exposes the interfaces IUnknown, A, B,C, and F. To provide the C interface, the class S3 encloses an S1 objectand exposes the C interface. To provide the F interface, the class S3encloses an S2 object and exposes the F interface. The S3 object exposesthe C and F interfaces by returning pointers to the C and F interfacesthrough the method QueryInterface of the controlling IUnknown interface.The D interface of the S1 object and the E interface of the S2 objectare external interfaces, but the S3 object does not expose theseinterfaces.

The methods S3::QueryInterface, S3::AddRef, and S3::Release compose thecontrolling IUnknown interface for the aggregate. The methodS3::QueryInterface returns a pointer to the controlling IUnknown, A, B,C, or F interfaces. When a pointer to the controlling IUnknown interfaceis returned, the method S3::QueryInterface invokes the method S3::AddRefto increment the reference count for the S3 object. The methodS3::AddRef increments the reference count, and the method S3::Releasedecrements the reference count and deletes the S3 object when thereference count is zero. When a pointer to the A, B, C, or F interfacesis returned, the method S3::QueryInterface invokes the method AddRef ofthe controlling IUnknown interface, which when the S3 object is notaggregated is the method S3::AddRef.

The global function CreateInstanceS3 creates an instance of an object ofclass S3. A client invokes this function to instantiate an object ofclass S3. Thus, a client can instantiate an object of class S3 withouthaving access to the S3 class definition at compile time or run time.The function CreateInstanceS3 is passed a pointer to the controllingIUnknown interface (punkOuter) when the instantiated S3 object isenclosed within an aggregate object and an identifier (iid) of aninterface exposed by the class S3 to return. The functionCreateInstanceS3 returns a pointer (ppv) to the identified interface.The function CreateInstanceS3 invokes the method S3::CreateInstancepassing the parameter punkOuter. The method S3::CreateInstanceinstantiates an S3 object and returns a pointer (ppunk) to the IUnknowninterface of the S3 object. The function CreateInstanceS3 then invokesthe method S3::QueryInterface to retrieve a pointer to the identifiedinterface. The function CreateInstanceS3 then invokes the methodS3::Release because the temporary pointer punk is no longer needed.

The method S3::CreateInstance instantiates an S3 object and returns apointer (ppunk) to the IUnknown interface of the S3 object. The methodS3::CreateInstance is passed a pointer (punkOuter) to the controllingIUnknown. The method S3::CreateInstance uses operator new to instantiatethe S3 object. During instantiation, the constructor S3::S3 is invokedand passed the value of the parameter punkOuter. After the S3 object isconstructed, the method S3::CreateInstance invokes the functionCreateInstanceS1 to create the enclosed S1 object. The methodS3::CreateInstance passes the parameter pS3->m_punkOuter and theinterface identifier for the IUnknown interface and is returned apointer to the IUnknown interface of the S1 object. The methodS3::CreateInstance stores the returned pointer in data memberS3::m_punkS1. The method S3::CreateInstance then invokes the functionCreateInstanceS2 to create an S2 object in a manner analogous to thecreation of the S1 object. The method S3::CreateInstance invokes themethod S3::QueryInterface to retrieve a pointer to the IUnknowninterface.

The method S3::AddRef increments the reference count of the S3 object.The method S3::Release decrements the reference count. When thereference counts is zero, the method S3::Release deletes the S3 object.

The constructor S3::S3 initializes the data members m_A, m_B,m_punkOuter, and m_refcount. The constructor S3::S3 is passed theparameter punkOuter. During instantiation of the data members m_A andm_B, the constructors A::A and B::B are invoked and passed the thispointer for the S3 object. If the value of the parameter punkOuter isNULL, the constructor S3::S3 sets the data member m₁₃punkOuter to thevalue of the this pointer (which points to the newly instantiated S3object). If the value of the parameter punkOuter is non-NULL, theconstructor S3::S3 sets the data member m_punkOuter to the value ofparameter punkOuter. Data member m_punkOuter points to the value of thecontrolling IUnknown interface of the aggregate when the S3 object isenclosed and points to the IUnknown interface of the S3 object when theS3 object is not enclosed. The constructor S3::S3 initializes the datamember m_refcount to zero.

The destructor S3::˜S3 invokes the method S1::Release to decrement thereference count of the enclosed S1 object. Since the reference count wasset to one during instantiation of the S1 object, the method S1::Releasedeletes the S1 object. The destructor S3::˜S3 decrements the referencecount of the S2 object in an analogous manner.

The methods of the A and B interfaces have an analogous behavior to themethods of the C interface. Thus, the A and B interface can be exposedwhen an S3 object is enclosed.

FIG. 5 is a block diagram showing the data structure layout of an S3object. The data structure layout comprises instance data 501, virtualfunction tables 502, 503, and 504, methods 505 through 517, andinstances of an S1 object 401-416 and an S2 object 519. The instancedata 501 contains a pointer to the virtual function table for thecontrolling IUnknown interface, data members m_A and m_B which areinstances of class A and B, data member m_punkOuter which points to theIUnknown interface of the S3 object, data member m_refcount whichcontains the reference count for the S3 object, data member m_punkS1which points to the IUnknown interface of the enclosed S1 object, anddata member m_punkS2 which points to the IUnknown interface of theenclosed S2 object 519. When the enclosed S1 object is instantiated, itsdata member S1::m_punkOuter is initialized to point to the IUnknowninterface of the S3 object. Similarly, when the enclosed S2 object isinstantiated, its data member S2::m_punkOuter is initialized to point tothe IUnknown interface of the S3 object

FIGS. 6A and 6B are block diagrams illustrating the cooperation betweenan enclosing object and an enclosed object. FIG. 6A is a block diagramillustrating an object of class S1 that is not enclosed within anotherobject. The class S1 object 601 includes data member m_punkOuter, whichpoints to the IUnknown interface and methods 603, 604, 605, and 606. Themethod IUnknown::QueryInterface 603 returns a pointer to the requestedinterface and increments the reference count. The methodsC::QueryInterface 605 and C::AddRef 606 invoke the corresponding methodsof the IUnknown interface. The implementation of the methods of class D(not shown) are analogous to those of class C. FIG. 6B is a blockdiagram illustrating an object of class S3 that encloses objects ofclass S1 and S2. The S2 object, which is analogous to the S1 object, isnot shown. The data member m_punkOuter 602 of the class S1 object 601points to the IUnknown interface of the class S3 object 610. The methodIUnknown::QueryInterface 613 returns a pointer to each of the exposedobjects and invokes the method IUnknown::QueryInterface 603 pointed toby data member m₁₃punkS1 619 to retrieve a pointer to the C interface.The data member m_punkOuter 612 points to the IUnknown interface of theclass S3 object 610. The methods QueryInterface 615 and 617 of the classA and B objects invoke the methods pointed to by data member m_punkOuter612.

In the above-described embodiment of the present invention, the methodQueryInterface of the controlling IUnknown interface of an aggregateinvokes the method QueryInterface of the IUnknown interface of enclosedobjects to retrieve pointers to the exposed interfaces. In an alternateembodiment of the present invention, an enclosing object can cachepointers to interfaces of enclosed objects that the enclosing objectexposes. Thus, when the method QueryInterface of the controllingIUnknown is invoked, the method can retrieve and return the cachedpointers after calling the method AddRef of the controlling IUnknowninterface, rather than invoke the method QueryInterface of the IUnknowninterface of the enclosed object. To implement this alternateembodiment, an enclosing object defines a data member for each cachedpointer. When the enclosed object is instantiated (typically duringconstruction of the enclosing object), the method QueryInterface of theIUnknown interface of the enclosed object is invoked to retrieve apointer of the exposed interface. It is preferred that the retrievedpointer is not reference counted so that the enclosing objecteffectively maintains only one pointer (e.g., S3::m_punkS1) to anenclosed object The enclosed object can then be deleted by a single callto the method Release. Therefore, after the pointer is cached, themethod Release of the exposed interface is invoked to remove thereference count attributable to the cached pointer.

In the above-described embodiment of the present invention, theimplementation of the method QueryInterface of the controlling IUnknowninterface includes a switch statement that specifies which interfacesare exposed. For example, the switch statement of the methodS3::QueryInterface includes a case label for each exposed interface A,B, C, F, and the controlling IUnknown. Thus, the exposed interfaces arestatically defined during implementation of the enclosing object. In analternate embodiment, the method QueryInterface of the controllingIUnknown interface can be implemented without specific knowledge of theexternal interfaces of the enclosed objects. When the methodQueryInterface is requested to return a pointer to an interface that itdoes not implement the method can invoke the method QueryInterface ofthe IUnknown interfaces of the enclosed objects to retrieve a pointer tothe identified interface, if implemented by an enclosed object. CodeTable 5 contains C++ pseudocode for a preferred implementation of themethod QueryInterface of the controlling IUnknown of a S3 object thatimplements this alternate embodiment. In addition to returning a pointerto each external interface of the enclosed objects, the methodQueryInterface of the controlling IUnknown could be implemented to notexpose certain external interfaces, while exposing all other externalinterfaces.

CODE TABLE 5 virtual boolean QueryInterface (REFIID iid, void **ppv) {ret = TRUE; switch (iid) { case IID_A: *ppv = &m_A; m_punkOuter—>AddRef(); break; case IID_B: *ppv = &m_B; m_punkOuter—>AddRef( ); break; caseIID_IUnknown; *ppv = this; AddRef( ); break; default: if(m_punkS1—>QueryInterface (iid, ppv)) { return ret;}; if(m_punkS2—>QueryInterface (iid, ppv)) { return ret;}; ret = FALSE; }return ret; }

In the above-described embodiments, error checking has not beendescribed. It is preferred that various types of error checking areperformed to ensure that an aggregate is properly created. For example,if an enclosing object tries to enclose an object that is notaggregatable, then the instantiation of the enclosing object should fail(e.g., the function CreateInstanceS1 returns a flag indicating failure).

In the above-described embodiments, an aggregate object can itself be anenclosed object within an enclosing object. This enclosing (nesting) canoccur to any depth. Alternately, an aggregate object can be implementedto be non-aggregable. The function CreateInstanceXX for the class XX canreturn a flag indicating a failure when the parameter punkOuter isnon-null, that is, when aggregation is desired.

In the above-described embodiment, an object for each external interfaceof an aggregable object is instantiated as a data member of theaggregable object. In an alternate embodiment, the external interfacesare inherited by the aggregable object, rather than implemented as datamembers of the aggregable object. Code Table 6 contains C++ pseudocodefor a preferred class definition S1 of an aggregable class with externalinterfaces C and D. The class S1 inherits the abstract classes IC andID. The implementations of the IC and ID interfaces need not store apointer to the derived class S1 to access the data member M_punkOuter,but a special, non-inherited implementation of the IUnknown interface(IUnknownS1) is needed. Conversely, the implementations of the IC and IDinterfaces, as shown in Code Table 3, store the pointer to the derivedclass S1 in the data member m_pS1. One skilled in the art wouldappreciate that other implementations using inheritance of interfacesare possible.

CODE TABLE 6 class S1 : public IC, public ID { public: virtual booleanQueryInterface (REFIID iid, void **ppv) { returnm_punkOuter—>QueryInterface(iid, ppv);} virtual void AddRef( ) {m_punkOuter—>AddRef( );} virtual void Release( ) { m_punkOuter—>AddRef();} // implementation of IC and ID private: class IUknownS1 : publicIUnknown { public: IUnknownS1 (S1 *pS1) { m_pS1 = pS1; m_refcount = 0;}virtual boolean QueryInterface (REFIID iid, void **ppv) {ret = TRUE;switch (iid) {case IID_UNknown: *ppv = this; AddRef( ); break; caseIID_C: *ppv = (IC *)m_pS1; m_pS1—>m_punkOuter—>AddRef( ); break; caseIID_D: *ppv = (ID *)m_pS1; m_pS1—>m_punkOuter—>AddRef( ); break;default: ret = FALSE;  }; } virtual void AddRef( ) {m_refcount++;}virtual void Release( ) {if(−−m_refcount == 0) delete m_pS1;} private:int m_refcount; S1 m_pS1; } friend IUnknownS1; IUnknownS1 m_IUnknownS1;public: static void CreateInstance (IUnknown *punkOuter, IUnknown**ppunk) { S1 *pS1 = new S1(punkOuter); pS1—>QueryInterface(IID_Unknown,ppunk); } private: void S1 (IUnknown *punkOuter) : m_IUnknownS1(this) {if(punkOuter == NULL) m_punkOuter = &m_IUnknownS1; else m_punkOuter =punkOuter; } IUnknown m_punkOuter; }

Dynamic Aggregation

In a preferred embodiment of dynamic aggregation, interface instancesare combined by adding them to an enclosing object at any time after thecreation of the enclosing object. In this manner, a new or changedinterface can be combined with an existing (base) object to alter theapparent behavior of the base object after the code for the base objecthas been compiled or linked. That is, although the behavior of the baseobject (as implemented by the methods of the base object) appearsoutwardly to have changed, the methods implementing the behavior of thebase object have not actually changed. The base object is enclosedwithin the enclosing object and the new or changed interfaces arethereafter added.

When an external request is made to access a particular interface, theenclosing object is responsible for determining which interface toreturn and how to invoke the requested interface if more than onematching interface exists. For example, if three IPrint interfaces existin the aggregate object, the enclosing object determines which IPrintinterface to return or whether to return its own IPrint interface, whichknows how to invoke a combination of the methods of the other IPrintinterfaces. The enclosing object can make this determination either froma fixed or specifiable set of combining rules.

These combining rules can be used to override the standard behavior ofan enclosed base object by providing access to a new implementation of apreviously defined interface of the enclosed base object. These rulescan also be used to enhance the behavior of an enclosed base object byadding capabilities not initially defined as part of the enclosed baseobject. Both override and enhancement capabilities are provided byadding a new or changed interface to the base object. In addition tothese capabilities, a standard enclosing object can implement defaultbehaviors for enclosed objects (interfaces that implement methods toinvoke if not provided for by the enclosed objects or added interfaces).Or, a standard enclosing object can implement controlling (overriding)behavior for a method typically present for all enclosed objects (suchas printing).

In a preferred embodiment, an object can be modified dynamically byallowing interface instances (implemented by objects) to be aggregatedtogether during the execution of a client program. The computer programthat instantiates an object is a client program. Aggregation is theprocess of combining the capabilities of several distinct objects byenclosing their respective interfaces within an enclosing object. Theenclosing object is then responsible for supporting access to allinterfaces it wishes to expose through the enclosing object'simplementation of a controlling IUnknown interface.

Static aggregation requires that the enclosing object have advanceknowledge of the interfaces (objects) it wishes to aggregate. Usingstatic aggregation, a programmer decides, in advance, which of itsaggregate object interfaces the enclosing object should expose and thenimplements the QueryInterface method of the controlling IUnknown of theenclosing object to return pointers to these exposed interfaces whenrequested. The QueryInterface method of the controlling IUnknownaccomplishes this task by maintaining references to the correspondingIUnknown interfaces of the individual enclosed objects. (Thesereferences are created when the enclosing object instantiates enclosedobjects.) When a request is received for a reference to an exposedinterface of one of the enclosed objects, the QueryInterface method ofthe controlling IUnknown invokes the corresponding IUnknown interface ofthe enclosed object to respond to the request. Because enclosed objectshave no knowledge of what interfaces the enclosing object exposes, allexternal requests received by an enclosed object are passed on to theenclosing object, thereby enabling access to the interfaces defined inthe other enclosed objects aggregated together.

The present invention also supports the dynamic aggregation ofinterfaces. In a preferred embodiment, an enclosing object provides amethod for registering instantiated interfaces and for later retrievingreferences to them. In addition, when an interface is requested from theaggregate object, the present invention provides a method for modifyingthe determination of which interface(s) to retrieve and how to invokethem in combination if more than one instance of the same interface ispresent in the aggregate object.

In a preferred embodiment, dynamic aggregation is implemented using amultitype object. A multitype object is an object capable of aggregatingobjects of varying types, hence its name. Only interfaces that have beencoded such that they are capable of being aggregated can be enclosedwithin a multitype object. (That is, for example, such interfaces canforward interface and reference counting requests to an enclosingobject.) A multitype object provides an IMultitype interface forrequesting the aggregation of particular interfaces or objects and foradding rules to determine how to invoke a requested interface. CodeTable 7 contains pseudocode for a preferred definition of the IMultitypeinterface.

CODE TABLE 7 class IMultiType: public IUnknown { virtual HRESULTAddObject (ULONG list, BOOLEAN headoflist, IUnknown *punkobj) = 0;virtual HRESULT AddInterface (REFIID iid, ULONG list, BOOLEANheadoflist, void **ppv = 0; virtual HRESULT AddRule (REFIID iid, IRULE*prule) = 0; virtual Enum (ULONG i; REFIID iid, ULONG list, BOOLEANheadoflist; void **ppv) = 0; }

FIGS. 7A, 7B, and 7C are block diagrams of the sequence of adding twoobjects to a multitype object. FIG. 7A is a block diagram of an instanceof a multitype object. The object MTO 7A01 implements an exposedinterface, the IMultitype interface MT, and a controlling IUnknown. Whenan external interface is added to the multitype object, the multitypeobject becomes an aggregate object. The multitype object implementationcontains three lists 7A02, 7A09, and 7A10 of interfaces it has added tothe aggregation. The multitype object uses these lists to invoke thevarious interfaces of its enclosed aggregate objects through themultitype object's controlling IUnknown interface. The multitype objectalso contains a list of rules 7A11 for accessing and combininginterfaces from the interface lists 7A02, 7A09, and 7A10.

The interaction of these different lists gives the multitype objectpowerful capabilities. The list of rules 7A11, which can be fixed orspecified using the AddRule method, specifies the interaction and use ofthe different interface lists for a particular interface. Hence, therecan be rules for selecting other rules as well as rules for selectingand combining particular interfaces. Three different interface lists7A02, 7A09, and 7A10 are provided in order to support override,enhancement, default, and controlling capabilities. When an interface isadded to the multitype object, the client program creating the aggregatespecifies the list to be used in adding the interface. List 7A02comprises the normal list, list 7A09 comprises the default list, andlist 7A10 comprises the override list. Basically, the override list isintended to implement override and controlling capabilities by pointingto interfaces that need to be accessed before the interfaces on thenormal list. The default list is intended to point to interfaces thatare accessed only when the override and normal lists do not contain arequested interface. The interaction of these lists is discussed ingreater detail in the description of the IRules interface.

FIG. 7B is a block diagram illustrating the multitype object MTO afteraggregating the IBasic interface using the AddObject method. TheAddObject method adds all of the interfaces of a specified object to amultitype object. The aggregate object MTO 7B01 comprises the multitypeinterface discussed with reference to FIG. 7A and an enclosedspreadsheet object S1 7B04. The enclosed object S1 implements aninstance of the external interface IBasic (B), an instance of theexternal interface IPrint (P), and an instance of IUnknown. (An externalinterface is an interface of an object that is exposed by an enclosingobject. An internal interface is an interface of an object that is notexposed by an enclosing object.) When the enclosed object S1 is added tothe normal list of the multitype object MTO, the normal list ofaggregated interfaces 7B02 contains a single element 7B03, whichidentifies the IUnknown interface of the enclosed object S1. The S1IUnknown interface returns pointers to the external interfaces B and Pupon request. Because S1 is aggregatable, when S1 is instantiated, it ispassed a pointer 7B05 to the enclosing object MTO, which can be usedsubsequently to access the other interfaces aggregated as part of objectMTO.

FIG. 7C is a block diagram illustrating the multitype object MTO of theresult after adding the IDatabase interface using the method AddObject.At this point, the aggregate object MTO 7C01 comprises the IMultitypeinterface, discussed with reference to FIG. 7A; an enclosed spreadsheetobject S1, discussed with reference to FIG. 7B; and an enclosed databaseobject S2 7C07, which implements database capabilities. The enclosedobject S2 implements an instance of the external interface IDatabase (D)and an instance of IUnknown. When the enclosed object S2 is added to themultitype object MTO using the method AddObject of the IMultitypeinterface, the normal list of aggregated interfaces 7C02 contains twoelements 7C03 and 7C06. Element 7C06 identifies the IUnknown interfaceof the enclosed object S2. Similar to S1, the S2 IUnknown interface isable to return a pointer to the external interface D and contains apointer 7C08 to the enclosing object MTO for access to the other MTOinterfaces.

One skilled in the art would recognize that many alternative embodimentsof the data structures used to keep track of the added interfaces andobjects are possible. For example, one could vary the number and kind oflists used. In particular, one could have only one list or make theoverride or default lists optional. Also, one could require that eachlist element only point to the precise interface to be aggregated andnot the IUnknown of the object when an entire object is aggregated (onlysupport an AddInterface style multitype object). Or, alternatively, onecould require that each list element point to the IUnknown of the objectregardless of what interface is added to the aggregation (only supportan AddObject style multitype object). In addition, one could use otherlist implementations including various sorted lists or hash tables ofinterface identifiers.

CODE TABLE 8 void CreateInstanceS1 (IUnknown *punkOuter, REFIID iid,void **ppv) { IUnknown *punk; S1::CreateInstance (punkOuter, &punk);punk—>QueryInterface (iid, ppv); punk—>Release ( ); } class IBasic:public IUnknown { virtual void File ( ) = 0; virtual void Edit ( ) = 0;virtual void Formula ( ) = 0; virtual void Format ( ) = 0; virtual voidGetCell ( ) = 0; } class IPrint: public IUnknown { virtual void Print(void **ppobj) = 0; } class S1: public IUnknown { public: static voidCreateInstance(IUnknown *punkOuter, IUnknown **ppunk) { S1 *pS1 = newS1(punkOuter); pS1—>QueryInterface(IID_IUnknown, ppunk); } private: voidS1(IUnknown *punkOuter) : m_B(this), m_P(this) { if (punkOuter == NULL)m_punkOuter = this; else m_punkOuter = punkOuter; m_refcount = 0; }class B: public IBasic { public: void B(S1 *pS1) {m_pS1 = pS1;} virtualboolean QueryInterface (REFIID iid, void **ppv) { returnm_pS1—>m_punkOuter—>QueryInterface(iid, ppv);} virtual void AddRef( ) {m_pS1—>m_punkOuter—>AddRef( );} virtual void Release( ) {m_pS1—>m_punkOuter—>Release( );} // other methods of IBasic includingFile, Edit, Formula, Format, GetCell private: S1 *m_pS1; } friend B; Bm_B; class P: public IPrint { public: void P(S1 *pS1) {m_pS1 = pS1;}virtual boolean QueryInterface (REFIID iid, void **ppv) { returnm_pS1—>m_punkOuter—>QueryInterface(iid, ppv);} virtual void AddRef( ) {m_pS1—>m_punkOuter—>AddRef( );} virtual void Release( ) {m_pS1—>m_punkOuter—>Release( );} // other methods of IPrint includingPrint private: S1 *m_pS1; } friend P; P m_P; public: virtual booleanQueryInterface (REFIID iid, void **ppv) { ret = TRUE; switch (iid) { case IID_B: *ppv = &m_B; m_punkOuter—>AddRef( ); break; case IID_P:*ppv = &m_P; m_punkOuter—>AddRef( ); break; case IID_IUnknown: *ppv =this; AddRef( ); break; default: ret = FALSE; } return ret; } virtualvoid AddRef( ) { m_refcount++;} virtual void Release( ) {if(--m_refcount == 0) delete this;} private: IUnknown *m_punkOuther; intm_refcount; }

Code Table 8 contains C++ pseudocode for a preferred class definition ofthe object S1 in FIGS. 7A-7C, which can be enclosed in an aggregate (anaggregatable object) along with a global function that creates aninstance of the S1 object. The classes IUnknown, IBasic, and IPrint areinterfaces that define the methods of each interface comprising S1. Theclass S1 implements the IUnknown interface, the IBasic interface, andthe IPrint interface. The IBasic and IPrint interfaces are implementedas external interfaces.

FIG. 8 is a block diagram of the data structure layout of an instance ofan object of class S1. Instance structure 801 contains the data membersof class S1 (m_B, m_P, m_punkOuter) and the virtual function tablepointer (S1::vfptr). The data members m_B and m_P are instances ofobjects of classes B and P, respectively (which are classimplementations of the interfaces IBasic and IPrint). Data members m_Band m_P are friends of class S1, which allows m_B and m_P objects toaccess the private members of class S1, such as m_punkOuter. The virtualfunction table pointer S1::vfptr points to virtual function table 802,the virtual function table pointer within data member m_B, S1::B::vfptr,points to virtual function table 803, and the virtual function tablepointer within data member m_P, S1::P::vfptr, points to virtual functiontable 804. Virtual function table 802 contains pointers to the virtualfunctions (methods) defined for the controlling IUnknown interface,virtual function table 803 contains pointers to the virtual functionsdefined for the IBasic interface, and virtual function table 804contains pointers to the virtual functions defined for the IPrintinterface. Methods 805 through 809 are the function members of class S1.Method 808 is the constructor for class S1. Method 809 is theCreateInstance method for class S1. Methods 810 through 818 are thefunction members of class B. Method 813 is the constructor for class B.Methods 819 through 823 are the function members of class P. Method 823is the constructor for class P. Because FIG. 8 shows an instance of anS1 object that is not part of an aggregate, the data membersS1::B::m_pS1, S1::P::m_pS1, and S1::m_punkOuter (pointers to theenclosing object) are initialized to point to the S1 object itself.

The object S1 as defined by Code Table 8 conforms to the requirementsfor an aggregatable object discussed with reference to staticaggregation. For the purposes of dynamic aggregation, Code Table 8illustrates how S1 can automatically communicate with its enclosingobject when it is aggregated and what is returned from the functionS1::QueryInterface. Specifically, upon creation of an S1 object, apointer to the controlling IUnknown interface of an enclosing multitypeobject is passed to the method CreateInstance. This pointer is then usedby the QueryInterface methods of S1's external interfaces (IBasic andIPrint) to route interface requests to the enclosing multitype object.When an S1 interface is requested from the enclosing multitype object,the method QueryInterface of the controlling IUnknown of the enclosingmultitype object invokes the method S1::QueryInterface, which returns apointer to the appropriate instance of the interface IBasic, theinterface IPrint, or the interface IUnknown and increments the S1object's reference counting appropriately. (The mechanism used by theenclosing multitype object to invoke S1::QueryInterface is discussed indetail below.)

One skilled in the art would recognize that many alternatives exist forpassing to an aggregatable object a pointer to the controlling IUnknowninterface of an enclosing multitype object. For example, instead ofpassing the pointer at creation time, a method can be definedspecifically for passing this pointer. Using this embodiment, an objectcan, once aggregated, be later unaggregated, or an object could later beaggregated into a different enclosing object.

To understand how Code Table 8 interacts with a multitype object asdepicted in FIGS. 7A-7C, it is helpful to see the calling sequence whenclient requests are made. Code Table 9 shows the pseudocode sequence ofcalls corresponding to FIG. 7C when a client application requests theIBasic interface when the client has a pointer to the multitype objectMTO.

CODE TABLE 9 MTO:: QueryInterface (IID_IBasic, ppv) which finds anaggregated object that supports the IBasic interface S1:: IUnknown::QueryInterface (IID_IBasic, ppv) which returns pointer to the Binterface

In the first call (MTO::QueryInterface), MTO determines from its listsof aggregated interfaces which object's QueryInterface method to invokeand then invokes it in the second call (S1::IUnknown::QueryInterface)

Code Table 10 shows how the pseudocode sequence of calls varies if theclient application has a pointer to one of the enclosed object'sinterfaces (such as the IPrint interface of S1) instead of a pointer tothe enclosing multitype object.

CODE TABLE 10 P::QueryInterface (IID_IBasic, ppv) which forwards thecall to the enclosing object MTO:: IUnknown:: QueryInterface(IID_IBasic, ppv) // m_punkOuter points to MTO:: IUnknown which finds anaggregated object that supports the IBasic interface S1:: IUnknown::QueryInterface (IID_IBasic, ppv) which returns pointer to the Binterface

Code Table 10 demonstrates how aggregation will automatically forwardrequests to the enclosing object in order to access other interfaceswithin the aggregate. In this case, the QueryInterface function of theenclosed object forwards the request to the enclosing object's (MTO's)QueryInterface method. Then, the MTO::QueryInterface method functions asin Code Table 9.

The S2 object that implements the IDatabase interface is analogous tothe S1 object as described above.

CODE TABLE 11 void CreateInstanceMTO (IUnknown *punkOuter, REFIID iid,void **ppv) { IUnknown *punk; MTO::CreateInstance (punkOuter, &punk);punk—>Query Interface (iid, ppv); punk—>Release ( ); } class IMultitype:public IUnknown { virtual HRESULT AddObject(ULONG list, BOOLEANheadoflist, IUnknown *punkobj) = 0; virtual HRESULT AddInterface (REFIIDiid, ULONG list, BOOLEAN headoflist, void *pv) = 0; virtual HRESULTAddRule (REFIID iid, IRule *prule) = 0; virtual HRESULT Enum (ULONG i,REFIID iid, ULONG list, BOOLEAN headoflist, void **ppv) = 0; } classMTO: public IUnknown { public: static void CreateInstance(IUnknown*punkOuter, IUnknown **ppunk) { MTO *pMTO = new MTO(punkOuter);pMTO—>QueryInterface(IID_IUnknown, ppunk);} private: void MTO(IUnknown*punkOuter): m_MT(this) { if (punkOuter == NULL) m_punkOuter = this;else m_punkOuter = punkOuter;} class MT: public IMultitype { public:void MT(MTO *pMTO) {m_pMTO = pMTO} virtual boolean QueryInterface(REFIID iid, void **ppv) { returnm_pMTO—>m_punkOuter—>QueryInterface(iid, ppv);} virtual void AddRef( ) {m_pMTO—>m_punkOuter—>AddRef( );} virtual void Release( ) {m_pMTO—>m_punkOuter—>Release( );} virtual boolean AddObject (ULONG list,BOOLEAN headoflist, IUnknown *punkobj) { item *pitem; pitem = new(item); pitem—>iid = IID_Unknown; pitem—>pobj = punkobj; pitem—>pnext =null; pitem—>pprev = null; switch (list) { case NORMAL_LIST; // . . . ifheadoflist == true, insert as first item in normal list, // otherwiseinsert as last item; case DEFAULT_LIST: // . . . if headoflist == true,insert as first item in default list, // otherwise insert as last item;case OVERRIDE_LIST: // . . . if headoflist == true, insert as first itemin override list, // otherwise insert as last item; default: // . . .insert at head of normal list; } } virtual boolean AddInterface (REFIIDiid, ULONG list, BOOLEAN headoflist, void *pv) { . . . pitem—>iid = iid;pitem—>pobj = pv; // same code as for AddObject method except that listitem points to // the particular interface and not to the IUnknowninterface . . . } \\ other methods of IMultitype . . . private: MTO*m_pMTO; }; friend MT; MT m_MT; public: virtual boolean QueryInterface(REFIID iid, void **ppv) { boolean done = TRUE; item *pitem; switch(iid) { case IID_IMultiType: *ppv = &m_MT; m_punkOuter—>AddRef( );break; case IID_IUnknown: *ppv = this; AddRef( ); break; default: //search through the override list for the first matching interface done =FALSE; pitem = m_poverride_itemhead; while ((done = = FALSE) &&(pitem—>pnext != null)) { switch (pitem—>iid) { case IID_IUnknown:if(pitem—>pobj—>QueryInterface(iid, ppv) = = TRUE) done = TRUE; elsepitem = pitem—>pnext; break; default: if (pitem—>iid = = iid) { ppv =pitem—>pobj; done = TRUE; } else pitem = pitem—>pnext; }} // searchthrough the normal list for the first matching interface // if not yetfound if (done = = FALSE) { pitem = m_pnormail_itemhead; while ((done == FALSE) && (pitem—>pnext != null)) { . . . // same code as for overridelist } // search through the default list for the first matchinginterface // if not yet found if (done == FALSE) { pitem =m_pdefault_itemhead; while ((done = = FALSE) && (pitem—>pnext != null)){ . . . // same code as for override list } break; } return done; }virtual void AddRef( ) { m_refcount++;} virtual void Release( ) {if(−−m_refcount = = 0) delete this;} private: IUnknown *m_punkOuter; intm_refcount, m_occurrence; struct item { REFIID iid; void *pobj; item*pnext; item *pprev }; item *m_pnormal_itemhead = null,*m_pnormal_itemtail = null, *m_pdefault_itemhead = null,*m_pdefault_itemtail = null, *m_poverride_itemhead = null,*m_poverride_itemtail = null; };

Code Table 11 is C++ pseudocode for a preferred class definition of amultitype object which can be used to dynamically aggregate-interfaces.The class MTO implements an instance of the IMultitype interface and thecontrolling IUnknown interface for the multitype object.

The global function CreateInstanceMTO creates an instance of an objectof class MTO. A client invokes this function to instantiate an object ofclass MTO. Using this function, a client can instantiate an object ofclass MTO without having access to the MTO class definition at compiletime or run time. The function CreateInstanceMTO is passed a pointer tothe controlling IUnknown interface (punkOuter) when the instantiated MTOobject is aggregated within another object. The function invokes themethod MTO::CreateInstance passing along the parameter punkOuter.

The method MTO::CreateInstance instantiates an MTO object and returns apointer (ppunk) to the IUnknown interface of the MTO object. Thisinterface can then be used by the function CreateInstanceMTO to returnthe interface actually requested by the client application. The methodMTO::CreateInstance uses the operator new to instantiate the MTO object.During instantiation, the constructor MTO::MTO is invoked and passed thevalue of the parameter punkOuter.

The constructor MTO::MTO initializes the data members m_MT andm_punkOuter. During instantiation of the data member m_MT of class MT,the constructor MT::MT is invoked and passed the this pointer for theMTO object. (In C++, the this pointer points to the object instanceitself.) The constructor MT::MT then sets a local variable pointing backto the MTO class. The constructor MTO::MTO is passed the parameterpunkOuter. If the value of punkOuter is null, the constructor MTO::MTOsets the data member m_punkOuter to point to the newly instantiated MTOobject. If, on the other hand, punkOuter is non-null, for example, ifthe object is aggregated as part of a larger aggregation, theconstructor MTO::MTO sets the data member m_punkOuter to the value ofthe parameter punkOuter. That is, data member m_punkOuter points to thevalue of the controlling IUnknown interface of the aggregate when theMTO object is enclosed and points to the IUnknown interface of the MTOobject when the MTO object is not enclosed

The IMultitype interface implemented by the MTO object contains fourmethods AddObject, AddInterface, AddRule, and Enum. The method AddObjectis responsible for adding an object to be enclosed within the multitypeobject (all of the object's interfaces are made accessible). It isdiscussed with reference to FIG. 9. The method AddInterface isresponsible for adding a single interface instance to the multitypeobject. The method AddInterface is discussed in conjunction with themethod AddObject. The method AddRule enables a client application tospecify a combining rule used to determine which combination of objectsto query or interfaces to return when a client application requests aparticular interface identifier. It is discussed in detail inconjunction with rule objects. The method Enum is used by rule objectsto enumerate over the various interface lists maintained by themultitype object. This method is discussed in detail in conjunction withrule objects.

FIG. 9 is a flow diagram of the method AddObject of the IMultitypeinterface implemented by a multitype object. FIG. 9 corresponds to thecode for AddObject shown in Code Table 11. The method AddObject is usedby a client application to dynamically add to a multitype object accessto all of the interfaces of an object. This method is passed a listindicator indicating to which list to add the object, an indication ofwhether the object should be added to the head or tail of the specifiedlist, and a pointer to the IUnknown interface of the object toaggregate. This method, along with the method AddInterface, implementsstructures for maintaining information regarding the objects andinterfaces enclosed by a multitype object.

One typical implementation uses three list structures composed ofelements each pointing to an interface of an enclosed object. When themethod AddObject is invoked to enclose the entire object, a new elementis added to the specified list structure; the new element points to theIUnknown interface of the enclosed object. This IUnknown interface canthen be used to access the component interfaces of the enclosed object.If, on the other hand, the method AddInterface is invoked to enclose asingle interface of an object, then a new list element is added to thespecified list structure; the new element points to the single interfaceto allow direct access to it. In a typical implementation, each listelement is indexed by an interface identifier, points to an interface ofan enclosed object, and points to the next element in the list. Sinceclients can add to either the head or tail of a list, a doubly linkedlist can be used to increase the efficiency.

In the method invocation, a client application specifies on which listthe application wants to add the specified interface or object. A“normal” list is used when the client application wants to simply addinterfaces or objects to the aggregation. An “override” list and a“default” list are used when the client application wants to addinterfaces whose methods will be invoked at a different time than thoseon the normal list. In a typical implementation, upon request for aparticular interface, the method QueryInterface of the controllingIUnknown of the multitype object will return the requested interfacesearching first through the override list, second through the normallist and third through the default list. One skilled in the art wouldrecognize that many other implementations and search strategies arepossible including varying the number of list structures, changing thesearch order, and changing the determination of what constitutesmatching a requested interface. In a preferred embodiment, as discussedin detail below, the client application may change the determinationrules.

The steps of FIG. 9 illustrate how an element is added to the specifiedlist. In step 901, the method allocates a new list item and, in step902, initializes the item to point to the IUnknown interface of theobject containing the interfaces the client application desires toaggregate and to contain the interface identifier of the item (toindicate the IUnknown interface). In step 903, the method determineswhether the normal list has been specified. If so, the method continuesat step 904, else it continues at step 907. In step 904, the methoddetermines whether the client application wants to insert an element atthe head of the normal list. If so, the method continues at step 905,else it continues at step 906. In step 905, the method inserts theinitialized element at the head of the normal list and returns. In step906, the method inserts the initialized element at the tail of thenormal list and returns. Steps 907 through 914 operate analogously onthe override and default lists.

The method AddInterface of the IMultitype interface works similarly tothe method AddObject. The primary difference is that, instead of anadded list element pointing to the specified IUnknown interface of theobject to be enclosed, the added list element points to a specifiedinterface and indicates the passed interface identifier. In this manner,a single interface of an object can be aggregated without exposing otherinterfaces.

Returning to Code Table 11, the methods QueryInterface, AddRef, andRelease of the IMultitype interface (the inherited IUnknown interface)forward requests to the IUnknown interface of the parent object thatimplements this IMultitype interface (MTO).

The controlling IUnknown interface implemented by the MTO objectcontains the methods QueryInterface, AddRef, and Release. The methodsAddRef and Release implement reference counting of the multitype object.When the reference count is zero, the MTO object is deleted.

FIG. 10 is a flow diagram of the method QueryInterface of thecontrolling IUnknown interface for a multitype object. FIG. 10corresponds to the code for QueryInterface shown in Code Table 11. Themethod QueryInterface locates a requested interface using knowledge ofits own implementation and information from the aggregated interfacelists. The method takes an input parameter which is the requestedinterface identifier and outputs a pointer to the requested interface.In steps 1001-1004, the method determines whether the requestedinterface is one implemented by the multitype object itself. Otherwise,in steps 1005-1014, the method searches each enclosed object orinterface until it finds the requested interface.

In step 1001, the method determines whether the requested interfaceidentifier is equivalent to IID_IMultitype, and if it is, continues atstep 1002, else continues at step 1003. In step 1002, the method setsthe output parameter to point to the instance of IMultitype implementedby the object MTO, and returns. In step 1003, the method determineswhether the requested interface identifier is equivalent to IID_IUnknownand, if it is, continues at step 1004, else it continues at step 1005.In step 1004, the method sets the output parameter to the this pointer,which is the instance of IUnknown implemented by the multitype object,and returns.

In steps 1005 through 1014, the method loops over the three listssearching for the first list element that points to an interfacematching the requested interface. When this interface is found, it isreturned in the parameter ppv and the method returns a successfulstatus. One skilled in the art would recognize that this implementationis one example of many types of searches that can be used. In step 1005,a temporary list indicator is set to the next list from the set of listsimplemented by a multitype object. In a preferred embodiment, this setof lists includes an override, a normal, and a default list. In step1006, the method sets a temporary variable pitem to point to the frontof the current list. In step 1007, the method determines whether it hasexhausted all of the elements in the current list and has still notfound a matching interface. If the method has reached the end of thecurrent list, then the method continues at step 1008, else it continuesat step 1009. In step 1008, if the method determines that more lists areavailable to be searched, then the method continues back at step 1005 tobegin searching a new list. Otherwise, the method returns anunsuccessful status since no matching interface was found. In step 1009,the method determines whether the current list element, pointed to bythe temporary variable pitem points to an IUnknown interface, and if itdoes continues at step 1010, else it continues at step 1013. (If thecurrent list element points to an IUnknown interface, then the objectcorresponding to this interface needs to be further queried for amatching interface.) In step 1010, the method calls the methodQueryInterface of the IUnknown interface pointed to by the current listelement. In step 1011, the method determines whether the requestedinterface identifier was found and, if not, continues at step 1012, elsereturns. If the requested interface identifier was found, then theQueryInterface call would have already set the return parameter ppv topoint to the requested interface. In that case, the method returns asuccessful status. Otherwise, in step 1012, the method increments thecurrent list element pointer (pitem) to point to the next element in thelist and continues at step 1007 to search through the current liststructure. In step 1013, the method compares the requested interfaceidentifier with the interface identification field of the current listelement and, if they are the same, continues at step 1014, elsecontinues at step 1012 with the search. In step 1014, the method setsthe return parameter ppv to point to the requested interface (pointed toby the current list element) and returns a successful status.

FIG. 11 is a block diagram showing the data structure layout of amultitype object, corresponding to FIG. 7C, after the IBasic, IPrint,and IDatabase interfaces have been dynamically aggregated using themethod AddObject. The data structure layout comprises instance data1101, virtual function tables 1104 and 1105, methods 1107 through 1115,and instances of currently enclosed (aggregated objects). The instancedata 1101 contains a pointer to the virtual function table for thecontrolling IUnknown interface, data member m_MT which is an instance ofclass MT (an implementation of the IMultitype interface), data memberm_punkOuter which points to the IUnknown interface of the MTO object,and data member m_pnormal_itemhead which points to the head of thenormal list of currently added interfaces. As depicted, the listcurrently contains two elements. List element 1102 points to an instanceof an S1 object 801-823 (as defined in FIG. 8) and list element 1103points to an instance of an S2 object 1106. Although not shown, themultiple object also contains pointers to a default list and an overridelist, which are empty.

When the enclosed S1 object is instantiated, its data memberS1::m_punkOuter is initialized to point to the IUnknown interface of theMTO object. Similarly, when the enclosed S2 object is instantiated, itsdata member S2::m_punkOuter is initialized to point to the IUnknowninterface of the MTO object. This task is accomplished, as previouslydiscussed, by passing a pointer to MTO in the CreateInstanceXX function(where XX is S1 or S2).

CODE TABLE 12 MTO  *pMTO; IMultiType *my_pMT; IUnknown *pSpreadSheet,*pDataBase; CreateInstanceMTO (NULL, IID_IUnknown, pMTO);CreateInstanceS1 (pMTO, IID_IUnknown, pSpreadSheet); CreateInstanceS2(pMTO, IID_IUnknown, pDataBase); pMTO—>QueryInterface (IID_Multitype,my_pMT); my_pMT—>AddObject (IID_ISpreadSheet, NORMAL_LIST, true,pSpreadSheet) my_pMT—>AddObject (IID_IDataBase, NORMAL_LIST, true,pDataBase); . . . // Some later time, some other client can invoke thedatabase capability of a spreadsheet objectpSpreadSheet—>QueryInterface(IID_IDataBase, ppv); ppv—>Data( );

Code Table 12 contains pseudocode for the preferred list of steps fordynamically aggregating a set of interfaces. This example corresponds toadding the interfaces for IBasic, IPrint, and IDatabase as shown in FIG.7C using the method AddObject to add all of the interfaces of an object.First, a new multitype object is allocated using the functionCreateInstanceMTO. Using this function, the IUnknown interface isretrieved for the newly instantiated multitype object. This interface ispassed to the S1 and S2 objects when they are instantiated to allow themto access the enclosing multitype object. At some point, an instance ofthe S1 object is created, passing it the pointer to the multitypeobject. Also, an instance of the S2 database object is created, passingit a pointer to the multitype object. After these instances of S1 and S2have been created, the IMultitype interface is retrieved from themultitype object. This interface will be used to dynamically add thespreadsheet and database interfaces to the multitype object. Next, themethod AddObject of the IMultitype interface of the multitype object isinvoked to aggregate the S1 and S2 objects into the multitype object.Once these objects have been enclosed in the multitype object, a clientcan use any interface of the multitype object or any of its enclosedobjects to access any other interface within the aggregate object. CodeTable 12 shows an example of retrieving the database interface using thebasic spreadsheet object (S1) and then invoking the data method of thisIDatabase interface.

CODE TABLE 13 class IMultitype: public IUnknown { virtual HRESULTAddObject (ULONG list, BOOLEAN headoflist, IUnknown *punkobj) = 0;virtual HRESULT AddInterface (REFIID iid, ULONG list, BOOLEANheadoflist, void *pv) = 0; virtual HRESULT AddRule (REFIID iid, IRule*prule) = 0; virtual HRESULT Enum (ULONG i, REFIID iid, ULONG list,BOOLEAN headoflist, void **ppv) = 0; } class IRule: public IUnknown {virtaul HRESULT Init (IMultitype *pMTO) = 0; } class MTO: publicIUnknown { public: static void CreateInstance(IUnknown *punkOuter,IUnknown **ppunk) { MTO *pMTO = new MTO(punkOuter);pMTO—>QueryInterface(IID_IUnknown, ppunk);} private: void MTO(IUnknown*punkOuter) : m_MT(this) { if (punkOuter == NULL) m_punkOuter = this;else m_punkOuter = punkOuter; pQI = new(MYQI);   // make aQueryInterface default rule pQI—>QueryInterface(IID_IRule, prule);m_MT—>AddRule(IID_IUknown, prule); } class MT: public IMultitype {public: void MT(MTO *pMTO) {m_pMTO = pMTO} virtual HRESULTQueryInterface (REFIID iid, void **ppv) { returnm_pMTO—>m_punkOuter—>QueryInterface(iid, ppv);} virtual void AddRef( ) {m_pMTO—>m_punkOuter—>AddRef( );} virtual void Release( ) {m_pMTO—>m_punkOuter—>Release( );} virtual HRESULT AddObject (ULONG list,BOOLEAN headoflist, IUnknown *punkobj) { item *pitem; pitem = new(item); pitem—>iid = IID_IUnknown; pitem—>pobj = punkobj; pitem—>pnext =null pitem—>pprev = null; switch (list) { case NORMAL_LIST: // . . . ifheadoflist = = true, insert as first item in normal list, // otherwiseinsert as last item; case DEFAULT_LIST: // . . . if headoflist = = true,insert as first item in default list, // otherwise insert as last item;case OVERRIDE_LIST: // . . . if headoflist = = true, insert as firstitem in override list, // otherwise insert as last item; default: // . .. insert at head of normal list; } } virtual RESULT AddInterface (REFIIDiid, ULONG list, BOOLEAN headoflist, void *pv) { . . . pitem—>iid = iid;pitem—>pobj = pv; // same code as for AddOjbect method except that listitem points to // the particular interface and not to the IUnknowninterface . . . } virtual HRESULT AddRule (REFIID iid, IRule *prule) //this method adds a rule object to the list of rules in the multitypeobject { ruleitem *pruleitem; pruleitem = new(ruleitem); pruleitem—>iid= iid; pruleitem—>prule = prule; pruleitem—>pnext = null;pruleitem—>pprev = null; // insert in the rule list -- one way to dothis is to insert the rule as last item // so QueryInterface is alwaysfirst . . . prule—>Init(m_pMTO);  // tell the rule about the multitypeobject } virtual HRESULT Enum (ULONG i, REFIID iid, ULONG list, BOOLEANheadoflist, void **ppv) // this method returns the i'th occurence of theelement corresponding to the // specified iid in the specified listbeginning with the head or tail of the list { int counter = 0; item*pitem = null; ruleitem *pruleitem = null; boolean done = FALSE; switch(list) { case NORMAL_LSIT: if (headoflist) { pitem =m_pMTO—>m_pnormal_itemhead; while ((!done) && (pitem ! = null)) { // foreach item in the list, compare either the pobj field if the // itempoints to an interface that has been added or query // interface theobject to see if the interface exists if the entire // object has beenadded switch (pitem—>iid) { case IID_IUnknown: if((pitem—>pobj—>QueryInterface(iid, ppv) = = TRUE) (counter = = i)) done= TRUE; else pitem=pitem—>pnext; break; default: if ((pitem—>iid = =iid) && (counter = = i)) { done = TRUE; ppv = pitem—>pobj; } else pitem= pitem—> pnext; break; }}; else { // . . . start from tail and workbackwards using // pitem = pitem—>pprev; }; break; case DEFAULT_LIST: //works the same as the normal list except uses the default list break;case OVERRIDE_LIST: // works the same as the normal list except uses theoverride list break; case RULE_LIST: if(headoflist) { pruleitem =m_pMTO—>m_prule_itemhead; done = FALSE; while ((!done) && (pruleitem ! =null)) { if pruleitem—>iid = = iid { ppv = pruleitem—>prule; done =TRUE; } else pruleitem = pruleitem—>pnext; } } else { // . . . startfrom tail and work backwards using // pruleitem = pruleitem—>pprev; };break; default: // use the same steps as for the normal list startingfrom the head } } private: MTO *m_pMTO; }; // end of class definitionfor MT object friend MT; MT m_MT; public: virtual HRESULTQueryInterface(REFIID iid, void **ppv) { IRule *prule; ruleitem*pruleitmem; boolean done = TRUE, foundrule = FALSE; switch (iid) { caseIID_IMultiType; *ppv = &m_MT; m_punkOuter—>AddRef( ); break; caseIID_IUnknown: *ppv = this; AddRef( ); break; default: done = FALSE; //search through the rule list for the first matching IUnknown interface// and invoke it as the selection rule to access the combining rule forthe // requested interface pruleitem = m_prule_itemhead; while((foundrule = = FALSE) && (pruleitem ! = null)) { if pruleitem—>iid = =IID_IUnknown { prule = pruleitem—>prule; foundrule = TRUE; } elsepruleitem = pruleitem—>pnext; } if (foundrule) { // get and call itsQueryInterface method prule—>QueryInterface(IID_IUnknown, pselect); done= pselect—> QueryInterface(iid, ppv); } else { // find the combiningrule on the rule list and return it if it exists // this code is thedefault selection rule if one is not provided pruleitem =m_prule_itemhead; foundrule = FALSE; while ((foundrule = = FALSE) &&(pruleitem ! = null)) { if pruleitem—>iid = = iid { prule =pruleitem—>prule; foundrule = TRUE; } else pruleitem = pruleitem—>pnext;} if (foundrule) { // find the requested interface and return it prule—>QueryInterface(iid, ppv); else // no combining rule exists so justreturn 1st interface found done = FALSE; // search through all lists forthe first matching interface if ((m_MT—>Enum(1, iid, OVERRIDE_LIST,true, ppv)) = = FALSE) {done = TRUE }; else if ((m_MT—>Enum(1, iid,NORMAL_LIST, true, ppv)) = = TRUE) { done = TRUE }; else if((m_MT—>Enum(1, iid, DEFAULT_LIST, true, ppv)) = = TRUE) { done = TRUE}; } break; } return done; } virtual void AddRef( ) { m_refcount++;}virtual void Release( ) {if (−−m_refcount == 0) delete this;} private:IUnknown *m_punkOuter; int m_refcount; struct item { REFIID iid; void*pobj; item *pnext; item *pprev }; item *m_pnormal_itemhead = null.*m_pnormal_itemtail = null, *m_pdefault_itemhead = null,*m_pdefault_itemtail = null, *m_poverride_itemhead = null,*m_poverride_itemtail = null; struct ruleitem { REFIID iid; IRule *pruleruleitem *pnext ruleitem *pprev }; ruleitem *m_prule_itemhead = null,*m_prule_itemtail = null; }; // end of class definition for MTO objectclass MyQI: public IUnknown { private: void MyQI: m_R(this) {any otherinitialization code } class R: public IRule { public: void R(MyQI*pMyQI) {m_pMyQI = pMyQI) } // . . . IUnknown methods are alsoimplemented here which call the controlling // IUnknown methods for theenclosing object (public methods shown below) virtual HRESULT Init(IMultitype* pMTO) {m_pMYQI—>m_pMTO = pMTO} private: MyQI  *m_pMyQI: };//end of class R definition friend R; R m_R; int m_refcount; iMultitype*m_pMTO; public: virtual HRESULT QueryInterface (REFIID iid, void **ppv){ IRule *prule; boolean done = TRUE, foundrule = FALSE; switch (iid) {case IID_IRule: *ppv = & m_R; AddRef( ); break; case IID_IUnknown: *ppv= this; AddRef( ); break; default: // this is the same as code for ourdefault rule for IUnknown // that knows how to search the lists of anMTO object foundrule = m_pMTO—>Enum(1, iid, RULE_LIST, true, prule); if(foundrule)   // return the combining rule for the requested IID{prule—> QueryInterface(iid, ppv);} else { // no combining rule existsso just return 1st interface found done = FALSE; // search through alllists for the first matching interface if ((m_pMTO—>Enum(1, iid,OVERRIDE_LSIT, true, ppv)) = = TRUE) {done = TRUE }; else if((m_pMTO—>Enum(1, iid, NORMAL_LIST, true, ppv)) = = TRUE) { done = TRUE}; else if ((m_pMTO—>Enum(1, iid, DEFAULT_LIST, true, ppv)) = = TRUE) {done = TRUE }; }; break; }; return done; } virtual void AddRef( ) {m_refcount++;} virtual void Release( ) {if(−−m_refcount == 0) deletethis;} };

Code Table 13 contains C++ pseudocode for a preferred class definitionor a multitype object that has been enhanced to support a list of ruleobjects. Recall that these rule objects can either contain combiningrules for combining requested interfaces of a multitype object or aselection rule for selecting a rule object (containing a combining rule)from the list of rule objects. Each rule object implements an instanceof the IRules interface for hooking the rule object into a multitypeobject. Each rule object also implements an instance of the interfacefor which the rule object is providing a combining rule. For example, tocoordinate printing all of the enclosed aggregated objects, a ruleobject that implements a combining rule for the IPrint interface can beprovided. Such a rule object contains an implementation of the IRulesinterface and an implementation of the IPrint interface. This specificexample is discussed further below with reference to Code Table 14.

Code Table 13 also shows C++ pseudocode for a preferred class definitionfor a rule object that contains a selection rule for selecting ruleobjects from a list of rule objects. To simplify the example, the ruleprovided by this rule object is similar to the default code used by amultitype object to access rule objects from the list of rule objects(as defined by MTO::QueryInterface). However, one skilled in the artwould recognize that this selecting rule object could implement any rulefor accessing rule objects.

As shown in Code Table 13, a multitype object implements two interfaces,IMultitype and IUnknown. These interfaces are essentially the same asthose described in Code Table 11, except, as noted in the followingdescription. The fundamental change from Code Table 11 shown in CodeTable 13 is that the method QueryInterface of the controlling IUnknown(MTO::QueryInterface) no longer searches through the three lists ofenclosed interfaces for a matching interface according to a fixed set ofrules. (The fixed rules shown in FIG. 11 amount to returning the firstfound interface from among the override, normal, and default lists inthat order.) Instead, once it determines that an interface is requestedthat is not implemented by the MTO object itself, MTO::QueryInterfacedefers the request to a rule object corresponding to the requestedinterface, if one exists on the MTO object's list of rule objects. Thefound rule object is then responsible for either providing animplementation of the requested interface or returning an interface fromthe MTO object's three lists of aggregated interfaces. Thus, a clientapplication can change the behavior of method QueryInterface of thecontrolling IUnknown of the aggregated object by providing its own ruleobjects for retrieving or combining particular interfaces. These ruleobjects can be implemented independent of the implementation of themultitype object because they can use a public enumeration methodprovided by the multitype object to access the enclosed interface andrule lists. In addition, a client application can change the selectionrule used by MTO::QueryInterface for finding the rule objectcorresponding to the requested interface by providing a rule object forthe IUnknown interface.

In addition to the methods AddObject and AddInterface already describedwith reference to Code Table 11, the class MTO provides two methods forhandling rule objects: AddRule and Enum. The method AddRule performssimilarly to AddInterface. The method AddRule creates a new list itemfor the list of rule objects and initializes this item to point to theinterface identifier and the IRule interface of the rule object passedas input parameters. The method then inserts the new list item in thelist of rule objects and invokes the method Init of the IRule interfacepointed to by the new list item in order to give the rule object accessback to the MTO object. Such access is used by the rule object to invokethe method Enum to access the other lists of interfaces.

As shown, the method AddRule adds a new rule object to the end of thelist of rule objects. In addition, the QueryInterface methods using thelist of rule objects assume that there is only one rule object perinterface identifier (or that rather the first one found is valid).Therefore, some error checking or order control is preferablyimplemented. One skilled in the art will recognize that, like the otherenclosed interface lists, a new rule object could be inserted intoeither the head or the tail of the list. Also, the method AddRule couldbe changed to pass in a parameter indicating where to add the new ruleobject similar to AddObject and AddInterface. Alternatively, the rulelist items could be implemented identically to the other list items andthe method AddInterface could be used to insert rule objects into thelist of rule objects. One skilled in the art would recognize that manyother techniques are possible and that, in an abstract sense, ruleobjects are also dynamically aggregated objects.

The class MTO provides the method Enum for searching and retrieving aspecified interface. Rule objects for combining rules can use thismethod to retrieve interfaces from the three aggregated interface lists.In addition, rule objects for selecting combining rules can use thismethod to retrieve rule object IRule interfaces from the list of ruleobjects. As shown, the method Enum returns the i'th occurrence of aninterface matching a requested interface identifier from a specifiedlist. The method starts from the head or tail of the list as specifiedin the invocation. In an alternate embodiment, the enumerator isimplemented as four separate methods: one per list enumerated.

For the normal, default, and override lists, the method Enum examineseach list item from the specified list until it finds the requestedinterface identifier or it exhausts the items in the list Duringexamination, if a list item points to an entire object, then the methodEnum invokes the method QueryInterface of the object pointed to by thelist item to potentially retrieve and return the requested interface.Otherwise, if the list item points to an interface other than IUnknown,the interface identifier pointed to by the list item is compareddirectly to the interface identifier requested, and, if they match, theinterface pointed to by the list item is returned. If a list item failsto produce a matching interface, then the method continues to search theremainder of the specified list.

The method Enum provides slightly different behavior for searching andretrieving from the list of rule objects. Instead of returning therequested interface identifier, the method uses the interface identifierto find a matching item on the list of rule objects and then returns apointer to the IRule interface of the rule object corresponding to therequested interface identifier.

In Code Table 13, the method QueryInterface of the MTO enclosing objecthas been modified to support the list of rule objects. The method firstdetermines whether the interface requested in an input parameter isimplemented by the MTO object itself. If so, the method returns thisinterface. Otherwise, the method retrieves and invokes a selecting ruleif one exists on the list of rule objects. Finally, if no selecting ruleexists, the method QueryInterface provides default selection code.

A rule object provides a selecting rule if it is added to the list ofrule objects under the IUnknown interface identifier. Preferably, thisinterface identifier is reserved for this purpose. In one embodiment,the first such rule object found is retrieved as the selecting rule. Ifit exists, the retrieved selecting rule is responsible for searching forand retrieving a combining rule or, if no combining rule exists,searching for and retrieving an interface from the three lists ofaggregated interfaces. The retrieved selecting rule is invoked byquerying the rule object corresponding to the IUnknown interfaceidentifier for its IUnknown interface (using the IRule interface) andthen invoking the method QueryInterface of this retrieved IUnknowninterface. Note that the retrieved IUnknown interface is the controllingIUnknown interface of the rule object.

If no selecting rule exists, the method MTO::QueryInterface providesdefault selection code. This default selection code returns a combiningrule for the requested interface if one exists, or returns the firstmatching interface found from the normal, override, and default lists inthat order. (The default selection code when no combination rule existsbehaves similarly to the method MTO::QueryInterface.) The rule objectimplementing the combining rule is then responsible for either providingan implementation of the requested interface or returning an interfacefrom the MTO object's three lists of aggregated interfaces.

As an example, Code Table 13 illustrates an implementation for a ruleobject that provides a selection rule for selecting rule objects fromthe list of rule objects. Class MyQI is a rule object implementing anIRule interface and an IUnknown interface. For ease of understanding,class MyQI is shown as a non-aggregatable object. The IRule interfaceprovides a method Init, for maintaining a pointer to the enclosingmultitype object (MTO) containing this rule object. This MTO pointer canbe later used to access the method Enum of the IMultitype interface inorder to access the aggregated interfaces and objects. The method Initis invoked by AddRule when a rule object is added to the list of ruleobjects. The IUnknown interface provides an implementation ofQueryInterface that knows how to select a combining rule object from thelist of rule objects. As shown, MyQI::QueryInterface provides theidentical functionality to that provided by MTO::QueryInterface.However, the implementation differs in that MyQI::QueryInterface usesthe method Enum to find a combining rule instead of searching the listitself. One skilled in the art would recognize that MTO::QueryInterfacecould have been implemented identically.

An example of using a MyQI rule object is shown in the method MTO::MTO.When the multitype object is instantiated, the constructor MTO::MTO isinvoked. This constructor creates a new rule object for the IUnknowninterface using the MyQI class definition and then adds this rule objectto the list of rule objects using the method AddRule. One skilled in theart will recognize that adding a rule object implementing a selectionrule can really be done at any time.

CODE TABLE 14 class IPrint: public IUnknown { virtual boolean Print(void **ppobj) = 0; } class myPrintRule: public IUnknown . . . private:. . . void myPrintRule: m_R(this), m_P(this) {/*any other initializationcode*/} class R: public IRule { public: void R(myPrintRule*pmyPrintRule) {m_pMyRule = pmyPrintRule) } // . . . IUnknown methodsare also implemented here which call the controlling // IUnknown methodsfor the enclosing object (public methods shown below) virtual HRESULTInit (IMultitype *pMTO) {m_pMyRule→m_pMTO = pMTO} private: myPrintRule*m_pMyRule; }; //end of class R definition friend R; R m_R; class P:public IPrint { public: void P(myPrintRule *pmyPrintRule) {m_pMyRule =pmyPrintRule) } // . . . IUnknown methods are also implemented herewhich call the controlling // IUnknown methods for the enclosing object(public methods shown below) virtual void Print (void * pobj) // calleach aggregated object print routine on each list in order of overriding// and then normal list and only call print routine from default list ifno print // routine exists on normal list { IID_IMultitype *p_iMT;IID_IPrint *p_iprint; int   i,j; p_iMT = m_pMyRule—>m_pMTO; for (i = 1;p_iMT—>Enum(i, IID_IPrint, OVERRIDE_LIST, true, &p_iprint); i++)p_iprint—>Print(pobj); for (j = 1; p_iMT—>Enum(j, IID_IPrint,NORMAL_LIST, true, &p_iprint); j++) p_print—>Print(pobj); if ((i= =1) &&(j= =1) && (p_iMT—>Enum(1,IID_IPrint, DEFAULT_LIST, true, &p_print)))p_print—>Print (pobj); }; } private: myPrintRule *m_pMyRule; }; friendP; P m_P; int m_refcount; IMultitype *m_pMTO; public: . . . virtualHRESULT QueryInterface(REFIID iid, void **ppv) { ret = TRUE; switch(iid) { . . . case IID_IPrint: *ppv = &m_P; AddRef( ); break; caseIID_IRule: *ppv = & m_R; AddRef( ); break; case IID_IUknown: *ppv =this; AddRef( ); break; . . . } return ret; } virtual void AddRef( ) {m_refcount++;} virtual void Release( ) {if(−−m_refcount == 0) deletethis;} };

Code Table 14 shows C++ pseudocode for an example of using a multitypeobject and a rule object to provide overriding behavior for a set ofaggregated objects or interfaces. Typically, when a set of objects ofdifferent types is aggregated, each object provides its own print methodwhich knows how to print the object Code Table 14 shows C++ pseudocodefor a preferred class definition of a rule object that provides acombination rule for the IPrint interface. The provided IPrint interfaceincludes a method Print, which invokes the print method of each enclosedobject or interface in an enclosing multitype object.

Class myPrintRule provides an implementation for at least twointerfaces, as do all rule objects: in this case, IRule and IPrint. TheIRule interface provided by class myPrintRule is functionally identicalto class MyQI, already discussed with reference to Code Table 13. TheIPrint interface simply provides a combining rule that enumeratesthrough the three lists of aggregated objects in a multitype objectsearching for other IPrint interfaces and invoking their Print routineswhen found. As discussed with reference to Code Table 13, after thisrule object is added to the list of rule objects in a multitype object,the method QueryInterface of the controlling IUnknown of the multitypeobject will preferably return a pointer to this combining rule IPrintinterface when a client application invokes QueryInterface requestingIID_IPrint on the aggregated object. As a result, when the clientapplication invokes the method Print, the method Print implemented bythis rule object will instead be invoked to ensure that the aggregatedobject prints its components correctly.

CODE TABLE 15 pPrintRule = new(myPrintRule); // make a IPrint interfacecombining rule pPrintRule—>QueryInterface(IID_IRule, prule);p_MTO—>QueryInterface(IID_IMultitype, p_MT); p_MT—>AddRule(IID_IPrint,prule); // add it to the Multitype object

Code Table 15 shows a C++ pseudocode sequence of calls that installs therule object defined in Code Table 14. First, a new instance of the classmyPrintRule rule object is created. Second, the IRule interface of thenew rule object is retrieved for later use. Third, after obtaining fromsomewhere a pointer to a multitype object (p_MTO), the IMultitypeinterface of the multitype object is retrieved. Finally, the methodAddRule of this retrieved IMultitype interface is invoked passing it theinterface identifier for the new rule object (IID_IPrint) and a pointerto the IRule interface of the new rule object.

In a preferred embodiment, an aggregated object is persistently storedusing the same mechanism illustrated for printing. Specifically, amultitype object provides a combination rule object for the IPersistFileinterface or for any other interface responsible for saving andretrieving objects. (The IPersistFile interface provides methods forsaving and loading objects.) In this manner, the multitype object isable to override the saving functionality of the enclosed objects andinterfaces to ensure that the whole aggregated object is saved. Thecombination rule preferably saves any data structures that the multitypeobject needs first and then invokes the method Save on each of theenclosed IPersistFile interfaces or objects. Thus, in order toparticipate in saving the aggregate object, a client program adds theIPersistFile interface of an object to be enclosed, or encloses theentire object No separate storage is needed in a separate file: themultitype object can store its own data in an analogous manner to howobject data is stored within a single file.

In an alternate embodiment that implements only one list of aggregatedinterfaces or objects and rules (instead of three lists plus the list ofrule objects), the multitype object needs no storage of its own. Nostorage aside from that used by the aggregated objects is necessarybecause the multitype object can recreate the single list by examiningthe ordering and indexing information of the aggregated objects in thehierarchical persistent storage structure. However, a multitype objectthat implements multiple interface lists typically needs to storeinformation to distinguish which interface instances are pointed to bywhich list.

The present embodiment of the current invention assumes that when thecontrolling IUnknown::QueryInterface method invokes the QueryInterfacemethods of the enclosed objects such invocation is performedsynchronously. That is, the enclosed object QueryInterface methodreturns before the controlling IUnknown::QueryInterface method invokesthe next enclosed object QueryInterface method. In an alternateembodiment, the controlling IUnknown::QueryInterface method calls theenclosed object QueryInterface methods asynchronously, not waiting fortheir return. Instead, standard interprocess communication mechanismsare used to inform the controlling routine when the enclosed objectmethod has completed. Such an embodiment is useful in a networking ormultiprocessor environment.

A typical application of the present invention for dynamic aggregationis for a user to combine instances of objects into a more powerfulobject using a program that has been developed with knowledge of dynamicaggregation. For example, suppose, using the spreadsheet interfaceexample presented in the background section, that a software vendorships a spreadsheet product from which a user can create aggregatablespreadsheet objects that only support the IBasic interface for basicmanipulation of a spreadsheet. (That is, using this product, a user cancreate instances of the IBasic interface, which are spreadsheetobjects.) Further suppose that another software vendor ships a databaseproduct from which a user can create aggregatable database queryobjects. These aggregatable database query objects support theDatabaseQuery interface, which for example includes a method DoQuery torun a query and a method DefineQuery to enable a user to specify aquery. When attached to other objects that can serve as input data,these database query objects can be invoked to query the attached inputdata. Also suppose that resident on the computer system is a program,for example a file manager, that manages objects and that knows how tocreate multitype objects and aggregate other objects and interfacestogether. To do this task, the file manager knows how to create amultitype object that provides an implementation of the IMultitypeinterface.

FIG. 12 is a pictorial representation of a spreadsheet object and adatabase query object, which can be aggregated together to create anattached database query object. To create a more powerful object thatcan perform a specified query on a specified database (the attacheddatabase query object 1205), the user first creates, using thespreadsheet product, an instance of a spreadsheet object 1201, which theuser wants to use as input data. Then, the user creates an instance of adatabase query object 1202 using the database product. The user theninvokes the method DefineQuery of the IDatabaseQuery interface bypressing the define button 1204 and enters text for the database queryusing a database language; for example, the SQL language. Next, the userinvokes the file manager 1208. Using the file manager, the user selectsthe spreadsheet object 1201 and the database query object 1202 andrequests that the file manager 1208 aggregate them. The user mightindicate this request, for example, by dragging and dropping the displayrepresentation of the spreadsheet object 1201 onto the data input port1203 of the display representation of the database query object 1202. Inresponse to the user's request, the file manager 1208 instantiates a newattached database query object 1205 by instantiating an enclosingmultitype object and invoking the method IMultitype::AddObject for boththe spreadsheet object 1201 and the database query object 1202. The filemanager 1208 also passes to both the spreadsheet object 1201 and thedatabase query object 1202 a pointer to the controlling IUnknowninterface of the newly instantiated multitype object. The user can theninvoke the attached database query object by selecting the “Go” button1206, to perform the specified query on the attached spreadsheet data.

One skilled in the art would recognize that once an object isaggregated, it is subject to the rules of the enclosing object. Thus,the behavior of spreadsheet object 1201 and the database query objectonce enclosed may change.

FIG. 13 is a block diagram of an aggregated attached database queryobject. The aggregated object 1304 corresponds to the attached databasequery object described with reference to FIG. 12. The aggregated object1304 comprises a multitype object 1301, a spreadsheet object 1302 and adatabase query object 1303. The multitype object 1301 is created, asdescribed above, in response to the user request to aggregatespreadsheet object 1302 and database query object 1303. During theprocess of aggregation, the multitype object creates pointers 1305 and1306 to the aggregated objects and passes to the objects to beaggregated a pointer to the controlling IUnknown interface. Later, whenthe user presses the “Go” button (1206 in FIG. 12), the controllingIUnknown interface of the multitype object 1307 is invoked to locate andinvoke the method DoQuery of the IDatabaseQuery interface of thedatabase query object 1303 using the above-described embodiments. Themethod DoQuery can then query for and invoke a (known) desired method ofthe spreadsheet object 1302 by searching for a (known) desired interfaceidentifier of the spreadsheet object 1302 using the methodQueryInterface. (To recall, the invocation of the method QueryInterfacewill get forwarded to method QueryInterface of the controlling IUnknown1307 the multitype object 1301, which will locate the desired interfaceif it exists.) Thus, the database query object 1303 is able to invokethe methods of the spreadsheet object 1302 without having access to thecompile time definitions of the interfaces of the spreadsheet object1302 as long as it knows the name (and parameters) of the method and theinterface identifier.

Although the present invention has been described in terms of apreferred embodiment, it is not intended that the invention be limitedto this embodiment. Modifications within the spirit of the inventionwill be apparent to those skilled in the art. The scope of the presentinvention is defined by the claims which follow.

What is claimed is:
 1. A method in a computer system of aggregating anenclosed object within an enclosing object, the enclosed object havingan object management interface and an external interface, the enclosingobject having a controlling object management interface, each interfacehaving a query function member for receiving an identifier of aninterface and for returning a reference to the identified interface, themethod comprising the steps of: creating an instance of the enclosingobject, wherein the query function member of the controlling objectmanagement interface of the enclosing object receives an identifier ofthe external interface of the enclosed object and returns a reference tothe external interface; and creating an instance of the enclosed object,wherein the query function member of the external interface of theenclosed object receives an identifier of an interface, invokes thequery function member of the controlling object management interface ofthe enclosing object passing the received identifier, and returns areference returned by the invoked query function member of thecontrolling object management interface of the enclosing object as areference to the identified interface.
 2. The method of claim 1 whereinthe enclosed object is an aggregate object.
 3. The method of claim 2wherein the aggregate object is not an aggregatable object.
 4. Themethod of claim 1 wherein the query function member of the controllingobject management interface of the enclosing object has no specificknowledge of the external interfaces of the enclosed object.
 5. Themethod of claim 1 wherein the query function member of the controllingobject management interface of the enclosing object has specificknowledge of an external interface of the enclosed object.
 6. The methodof claim 1 wherein the query function member of the controlling objectmanagement interface of the enclosing object invokes the query functionmember of the object management interface of the enclosed object toretrieve a reference to an external interface of the enclosed object. 7.The method of claim 1 wherein the enclosing object stores a reference tothe external interface of the enclosed object and wherein the queryfunction member of the controlling object management interface of theenclosing object returns the stored reference as the reference to theexternal interface of the enclosed object.
 8. A method in a computersystem of implementing a class defining an aggregatable object, theclass having an object management interface and an external interface,the object management interface and external interface having a queryfunction member for providing access to the interfaces, the methodcomprising the steps of: generating a create instance function memberfor instantiating an object of the class, wherein the create instancefunction member stores in a data member of the class a reference to theobject management interface of the class when the object is notaggregated and stores in the data member of the class a reference to thecontrolling object management interface of an enclosing object when theobject is aggregated; generating a query function member of the objectmanagement interface, the query function member for receiving anidentifier of an interface of the object and returning a reference tothe identified interface; and generating a query function member of theexternal interface, the query function member for receiving anidentifier of an interface, invoking the query function member of theobject management interface referenced by the data member passing theidentifier of the interface, and returning a reference returned by theinvoked query function member.
 9. The method of claim 8, including thestep of generating a global create instance function for instantiatingan object of the class, wherein the global create instance functioninvokes the create instance function member to create an instance of theclass and invokes the query function member of the object managementinterface to retrieve a reference to an external interface of the classwhereby the global create instance function is invocable by a client tocreate an instance of the class without the client needing access to theclass definition of the class.
 10. A method in a computer system ofcreating an aggregate object, the aggregate object having an enclosedobject, the enclosed object having an external interface that is exposedby the aggregate object, the method comprising the steps of:instantiating the aggregate object, the aggregate object having a queryfunction member for retrieving references to interfaces exposed by theaggregate object; instantiating the enclosed object, the enclosed objecthaving a query function member of the external interface for retrievingreferences to interfaces exposed by the aggregate object; storing areference to the enclosed object, wherein the query function member ofthe aggregate object uses the stored reference to the enclosed object toretrieve a reference to the exposed external interface; and storing areference to the aggregate object, wherein the query function member ofthe external interface uses the stored reference to the aggregate objectto retrieve a reference to an interface exposed by the aggregate object.11. The method of claim 10 wherein the stored reference to the enclosedobject is a reference to an object management interface of the enclosedobject having a query function member and the query function member ofthe aggregate object invokes the query function member of the referencedobject management interface to retrieve a reference to the exposedexternal interface.
 12. The method of claim 10 wherein the storedreference to the enclosed object is a reference to the externalinterface of the enclosed object and wherein the query function memberof the aggregate object uses the reference to the external interface asthe reference to the exposed external interface.
 13. The method of claim10 wherein the query function member of the external interface uses thestored reference to the aggregate object to invoke the query functionmember of the aggregate object to retrieve a reference to an interfaceexposed by the aggregate object.
 14. The method of claim 13 wherein thestored reference to the enclosed object is a reference to an objectmanagement interface of the enclosed object having a query functionmember and wherein the query function member of the aggregate objectinvokes the query function member of the reference object managementinterface to retrieve a reference to an exposed external interface. 15.A method in a computer system of enclosing an enclosed object within anenclosing object, the enclosing object having an query function member,the enclosed object having an query function member, the query functionmembers for retrieving references to interfaces exposed by the enclosingobject, the enclosed object having an external interface that is exposedby the enclosing object the query function member of the enclosed objectbeing associated with the external interface of the enclosed object andthe enclosed object having an object management interface with a queryfunction member, the method comprising the steps of: instantiating theenclosing object; instantiating the enclosed object; storing a referenceto the enclosing object within the enclosed object; and when executingthe query function member associated with the external interface of theenclosed object, invoking the query function member of the enclosingobject to retrieve a reference to an exposed interface of the enclosingobject using the stored reference to the enclosing object, wherein thequery function member of the enclosing object invokes the query functionmember of the object management interface of the enclosed object toretrieve a reference to an external interface of the enclosed object.16. A method in a computer system of implementing a class defining anaggregatable object, the class having an object management interface andan external interface, the object management interface and the externalinterface having query function members for providing access to theinterfaces of the aggregatable object, the method comprising the stepsof: defining the external interface; and defining the aggregatableclass, the aggregatable class inheriting the defined external interfaceas a base class, the aggregatable class having an implementation of thequery function member of the defined external interface, wherein thequery function member of the defined external interface invokes thequery function member of the object management interface when the objectis not aggregated and invokes a query function member of an aggregatingobject when the aggregatable object is aggregated.
 17. An aggregatableobject in a computer system comprising: an object management interface,the object management interface having a query function member forreceiving an identifier of an interface of the object and returning areference to the interface; an external interface, the externalinterface having a query function member for receiving an identifier ofan interface, invoking the query function member of the objectmanagement interface when the object is not aggregated and invoking aquery function member of an enclosing object when the object isaggregated, and returning a reference to the identified interface.
 18. Amethod in a computer system for adding an interface to an object, theinterface implemented by an object being able to be enclosed into anenclosing object, the method comprising the steps of: creating aninstance of the enclosing object, the enclosing object having an addinterface function member for adding an interface to the enclosingobject and a query function member for retrieving a reference to anadded interface; creating an instance of an object to be enclosed withinthe enclosing object, passing a reference to the enclosing object, theobject to be enclosed having an interface and having a query functionmember for retrieving a reference to the interface; and invoking the addinterface function member of the enclosing object, passing a referenceto the object to be enclosed, thereby enclosing the object and addingthe interface of the enclosed object to the enclosing object, wherebywhen the query function member of the enclosing object is invoked, thequery function member of the enclosing object returns a reference to theadded interface of the enclosed object.
 19. The method of claim 18,wherein the invocation of the query function member of the enclosingobject is performed asynchronously.
 20. The method of claim 18, whereinthe invocation of the query function member of the enclosing object isperformed synchronously.
 21. The method of claim 18, wherein theenclosing object provides a default interface.
 22. The method of claim18, wherein the enclosed object provides a default interface.
 23. Themethod of claim 18, wherein the enclosing object provides an interfacethat overrides the added interface of the enclosed object.
 24. Themethod of claim 18, wherein the enclosing object has no knowledge of theinterfaces exposed by the enclosed object.
 25. A method in a computersystem for adding a function member to an object, the method beinglanguage independent and comprising the steps of: creating an instanceof an enclosing object, the enclosing object having an add functionmember for adding a function member to the enclosing object and a queryfunction member for retrieving a reference to an added function member;creating an instance of an object to be enclosed within the enclosingobject, passing to the object to be enclosed a reference to theenclosing object, the object to be enclosed having an implementation ofa function member to be added to the enclosing object and having a queryfunction member for retrieving a reference to the function member to beadded; and invoking the add function member of the enclosing object,passing a reference to the object to be enclosed, thereby enclosing theobject and adding the function member of the enclosed object, wherebywhen the query function member of the enclosing object is invoked, thequery function member returns a reference to the added function memberof the enclosed object.
 26. A computer system for adding an interface toan object, the system comprising: an enclosing object having an addinterface function member for adding an interface to the enclosingobject and a query function member for retrieving a reference to anadded interface; and an object to be enclosed with the enclosing object,the object to be enclosed having an interface and a query functionmember for retrieving a reference to the interface, wherein the objectis enclosed and the interface added by invoking the add interfacefunction member of the enclosing object passing it a reference to theobject to be enclosed, and whereby when the query interface functionmember of the enclosing object is invoked after the object is enclosed,the query interface function member returns a reference to the addedinterface of the enclosed object.
 27. A method in a computer system foradding an interface to an enclosing object, after instantiation of theenclosing object, the method comprising the step of aggregating anobject into the enclosing object, thereby enclosing the object, theenclosed object having an exposed interface, wherein the enclosed objectforwards requests for an interface to the enclosing object, a referenceto the exposed interface being accessible to the enclosing object, andwherein when the enclosing object receives a request for a reference tothe exposed interface, the enclosing object returns the reference.
 28. Amethod in a computer system for combining a plurality of interfaces withan enclosing object, after instantiation of the enclosing object, themethod comprising the steps of: aggregating a first object into theenclosing object, thereby enclosing the first object, the enclosed firstobject having an exposed interface, wherein the enclosed first objectforwards requests for an interface to the enclosing object; andaggregating a second object into the enclosing object, thereby enclosingthe second object, the enclosed second object having an exposedinterface, a reference to the exposed interface of the enclosed secondobject being accessible to the enclosing object, and wherein when theenclosed first object receives a request for a reference to the exposedinterface of the enclosed second object, the enclosed first objectforwards the request to the enclosing object and the enclosing objectreturns the reference to the exposed interface of the enclosed secondobject.
 29. A method in a computer system for adding an interface to anobject after the object is instantiated, the method comprising the stepsof: instantiating an enclosing object, the enclosing object forreceiving a reference to an interface, for storing the reference, andfor returning the stored reference when requested; instantiating anenclosed object, the enclosing object having a reference to the enclosedobject and having an interface; and sending to the enclosing object areference to the interface, whereby the enclosing object receives thesent references, stores the sent reference, and returns the storedreference when requested.
 30. The method of claim 29, wherein when theenclosed object receives a request for an interface, it forwards therequest to the enclosing object.